ios-Lazy Loading

Source: Internet
Author: User

Lazy loading, also known as deferred loading. The popular point is that in development, when the application needs to use the resources. Do not load resources when the program is started, and then load the resources only when they are running when some resources are needed.

We know that iOS devices have limited memory, and if you load all the resources that will be used in the future once the program is started, you may run out of memory on your iOS device. These resources such as large amounts of data, pictures, audio, etc.

Here's an example:

1> Define control Properties, note: The property must be strong, the sample code is as follows:

@property (nonatomic, strong) Nsarray *imagelist;

2> lazy Loading in the getter method of the property, the sample code is as follows:

//lazy Loading-when needed, is loaded into memory during instantiation-(Nsarray *) imagelist{//only when the getter method is called for the first time is empty, and the array is instantiated at this time    if(_imagelist = =Nil) {        //File means loading files from the full path of the fileNSString *path = [[NSBundle mainbundle] Pathforresource:@"ImageData"OfType:@"plist"]; NSLog (@"%@", path); _imagelist=[Nsarray Arraywithcontentsoffile:path]; }        return_imagelist;}

As the above code, there is a _imagelist property, if in the code of the program, there are multiple accesses to the _imagelist property, such as the following

self.imagelist; self.imagelist; self.imagelist;

Although the _imagelist property was accessed 3 times, the imageList array is not empty when the imageList zodiac is accessed for the first time.
ImageList! = nil When accessing imageList for the second time; The program will not execute the following code

NSString *path = [[NSBundle mainbundle] Pathforresource:@ "ImageData" ofType:@ " plist " ];        NSLog (@ "%@", path);                 = [Nsarray Arraywithcontentsoffile:path];

The data will not be loaded in the Plist file again.

Benefits of Lazy Loading:

1> do not have to write the code of the object in the Viewdidload method, the code is more readable

2> Each property's getter method is responsible for the respective instantiation, the code is independent of each other, loosely coupled

3> only when resources are really needed, and then load, saving memory resources.

Reminder: This is what Apple is advocating. In fact, Apple's iOS system has been used in many parts of the way lazy loading, such as the creation of the controller's view.

Later on the blog, I will involve, welcome to communicate.

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.