IOS Lazy Load Mode

Source: Internet
Author: User

iOS Development-Lazy loading

1. Lazy loading-Also known as deferred loading-is loaded when needed (low efficiency, small memory footprint). The so-called lazy load, write is its get method.

Note: If it is lazy to load the words must be careful to determine whether there is already, if not then go to alloc Init

2. We know that iOS devices have limited memory, and if you load all the resources that will be used in the future after the program starts, 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) Uiimageview *icon;

@property (nonatomic, strong) UIButton *nextbtn;

@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, then instantiate the load into memory

/*** lazy loading of picture controls ***/

-(Uiimageview *) icon

{

Determine if there is already, and if not, instantiate

if (!_icon) {

_icon=[[uiimageview alloc]initwithframe:cgrectmake (x, Y, W, h)];

UIImage *image=[uiimage imagenamed:@ "icon"];

_icon.image=image;

[Self.view Addsubview:_icon];

}

return _icon;

}

/*** delay loading of the button ***/

-(UIButton *) nextbtn

{

Determine if there is already, and if not, instantiate

if (!_NEXTBTN) {

_nextbtn=[uibutton Buttonwithtype:uibuttontypecustom];

_nextbtn.frame=cgrectmake (0, Self.view.center.y, 40, 40);

[_nextbtn setbackgroundimage:[uiimage imagenamed:@ "normal"] forstate:uicontrolstatenormal];

[_nextbtn setbackgroundimage:[uiimage imagenamed:@ "highlighted"] forstate:uicontrolstatehighlighted];

[Self.view ADDSUBVIEW:_NEXTBTN];

[_nextbtn addtarget:self Action: @selector (Nextclick:) forcontrolevents:uicontroleventtouchupinside];

}

return _nextbtn;

}

/*** The Get method of the array ***/

-(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 file

NSString *path = [[NSBundle mainbundle] pathforresource:@ "ImageData" oftype:@ "plist"];

_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.
When you access imageList for the second time imageList! = nil, the data is not loaded again in the Plist file.

3. Benefits of using lazy loading:

(1) You do not have to write all the code of the created object in the Viewdidload method, the code is more readable

(2) Each control's getter method is responsible for the respective instantiation processing, the code is independent of each other, loosely coupled

(3) Only when the resources are really needed , then load, save the memory resources.

IOS Lazy Load Mode

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.