How to use lazy load in iOS development-ui

Source: Internet
Author: User
Tags memory usage

1. Lazy Load Basic

Lazy Loading--also known as deferred loading--is loaded when it is needed (inefficient, small memory footprint). The so-called lazy load, write is its getter method. The popular point is that in the development, when the program needs to use resources. The resource is not loaded when the program is started, only when it is run when some resources are needed.

We know that the memory of iOS devices is limited, and if you load all the resources that will be used in the future after the program starts, it is possible to deplete the memory of the iOS device. These resources such as a lot of data, pictures, audio and so on, so we use lazy load must be careful to determine whether there is already, if not then to instantiate


2. The advantages of using lazy loading:

(1) Do not have to write all the code to create the object in the Viewdidload method, the code is more readable

(2) Each control's getter method is responsible for the respective instantiation process, the code is independent of each other strong, loose coupling

(3) Save the memory resources only when the resource is really needed, then load it.


3. code example

For example, the application of the login interface is usually a combination of lable and TextField we customize a Ltview class containing titlelable attributes and TextField attributes

Then we can complete the lazy loading (lazy load) mode by overriding the property's Getter method, using lazy loading can encapsulate the code according to the module, increase the flexibility of the class, and save memory usage for a certain period of time, for the current Ltview, Using lazy loading indicates that I provide two views, and if so, order calls getter methods can display the child view, and if not, Ltview is an empty view

Reminder: This is what apple advocates. In fact, many parts of Apple's iOS system use lazy loading, such as the creation of a controller's view.

Related Article

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.