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.