Lazy loading and ios lazy loading

Source: Internet
Author: User

Lazy loading and ios lazy loading

Definition of lazy loading:
 
The so-called lazy loading can be defined:
Delayed loading, that is, loading when the object needs to be used. This is actually the so-called get Method for rewriting objects,
When the system or developer calls the get method of an object, it loads the object.
 
Advantages of lazy loading:
 
1. You do not need to instantiate the object to viewDidLoad, which simplifies the code and enhances code readability.
 
2. Object Instantiation in the getter method, perform their respective duties to reduce Coupling
 
3. The memory usage of the system will be reduced. Only when resources are actually needed can the system be loaded again, saving memory resources.

Notes for lazy loading:
 
1. When rewriting the get method, first determine whether the object is empty. If it is empty, then instantiate the object.
 
2. Do not use self. shopData in the getter method, because self. shopData will call the getter method, resulting in an endless loop.
 
When to use lazy loading:
 
1. Why should I use lazy loading:

(1) This is what Apple advocates. In fact, many IOS systems made by Apple use lazy loading methods, such as the creation of the controller View.
 
(2) 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. When to use:
Not all getters need to be written for lazy loading. Sometimes you are sure that the instance variables need to be initialized somewhere, so you do not need to override getter.
. A large number of getter rewriting will cause a lot of layout waste and increase the input. For example, all the la S and
View Initialization is required, so you can put these controls into an independent method for initialization and layout, and do not need to write getter separately.

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.