How to use lazy loading in iOS development UI and ios development ui

Source: Internet
Author: User

How to use lazy loading in iOS development UI and ios development ui

 

1. Basic lazy loading

Lazy loading, also known as delayed loading, is loaded only when needed (low efficiency and low memory usage ). The so-called lazy loading write is its getter method. To put it bluntly, it is to say that in development, when the resources need to be used in the program. Resources are not loaded when the program starts. These resources are loaded only when some resources are required for running.

We know that the memory of iOS devices is limited. If you load all the resources that will be used in the future at one time after the program starts, the memory of iOS devices may be exhausted. These resources, such as a large amount of data, images, audios, and so on, make sure to determine whether there is any lazy loading. If not, instantiate it.

2. Benefits of using lazy loading:

(1) do not write all the code for creating objects in the viewDidLoad method, and the code is more readable.

(2) The getter method of each control is responsible for instantiation, and the code is highly independent and loosely coupled.

(3) only when resources are actually needed can they be loaded, saving memory resources.

3. Sample Code

For example, the application login interface is usually a combination of Lable and textField. We customize an LTView class that includes the titleLable attribute and textField attribute.

Then, we can use the getter method to override the attribute to complete the lazy loading mode. By using lazy loading, We can encapsulate the code according to the module and improve the flexibility of the class, you can also save memory usage for a certain period of time. For the current LTView, lazy loading indicates that I provide two subviews, call the getter method in order to display this subview. If you do not need this subview, LTView is an empty view.

 

 

Note: This is what Apple advocates. In fact, many IOS systems made by Apple use lazy loading methods, such as creating a controller 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.