Lazy Loading in iOS

Source: Internet
Author: User

    • lazy loading can be defined as: delay loading , that is, when the object needs to be used to load again. It is actually called the Get method of the rewrite object , and when the system or developer invokes the object's Get method, the object is loaded.
    • Advantages of lazy Loading
    • You can simplify the code and improve the readability of your code without having to write an instantiation of the object to Viewdidload
      • Instantiation of objects in Getter methods, with their respective roles, to reduce coupling
      • The memory usage of the system is reduced
Viewdidload Normal Load code example
    • When lazy loading, get the data from Plist, return an array, need to write in the Viewdidload method to get
 @interface viewcontroller ()  @property ( Nonatomic, strong) nsarray *shopdata; @end  @implementation  viewcontroller-(void) viewdidload {[super ViewDidLoad]; _ Shopdata = [nsarray arraywithcontentsoffile:[[NSBundle Mainbundle] Pathforresource:@ "shop" Oftype:@ "plist"];  @end             
    • Obviously, when the controller is loaded, it will load the current shopdata, if the shopdata is triggered when certain events will be called, there is no need to get the controller after loading the plist file, if the event is not triggered, it means that shopdata will never be used, Loading the Shopdata in the viewdidload is redundant and consumes memory
Lazy Load code example
- (void)viewDidLoad {    [super viewDidLoad];}- (NSArray *)shopData{    if (!_shopData) {        _shopData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]]; } return _shopData;}@end
    • When a shopdata is needed, the [self Shopdata] Method (The getter method) is called, and the Getter method is called, and then the getter method gets the contents of the Plist file. Then return to use (note in Getter method do not use Self.shopdata, because Self.shopdata will invoke getter method, resulting in a dead loop) Summary: Lazy loading is used to load the object

Lazy Loading in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.