- lazy loading can be defined as: delay loading , that is, when the object needs to be used to load again. It is actually called the Get method of the rewrite object , and when the system or developer invokes the object's Get method, the object is loaded.
- Advantages of lazy Loading
- You can simplify the code and improve the readability of your code without having to write an instantiation of the object to Viewdidload
- Instantiation of objects in Getter methods, with their respective roles, to reduce coupling
- The memory usage of the system is reduced
Viewdidload Normal Load code example
- When lazy loading, get the data from Plist, return an array, need to write in the Viewdidload method to get
@interface viewcontroller () @property ( Nonatomic, strong) nsarray *shopdata; @end @implementation viewcontroller-(void) viewdidload {[super ViewDidLoad]; _ Shopdata = [nsarray arraywithcontentsoffile:[[NSBundle Mainbundle] Pathforresource:@ "shop" Oftype:@ "plist"]; @end
- Obviously, when the controller is loaded, it will load the current shopdata, if the shopdata is triggered when certain events will be called, there is no need to get the controller after loading the plist file, if the event is not triggered, it means that shopdata will never be used, Loading the Shopdata in the viewdidload is redundant and consumes memory
Lazy Load code example
- (void)viewDidLoad { [super viewDidLoad];}- (NSArray *)shopData{ if (!_shopData) { _shopData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]]; } return _shopData;}@end
- When a shopdata is needed, the [self Shopdata] Method (The getter method) is called, and the Getter method is called, and then the getter method gets the contents of the Plist file. Then return to use (note in Getter method do not use Self.shopdata, because Self.shopdata will invoke getter method, resulting in a dead loop) Summary: Lazy loading is used to load the object
Lazy Loading in iOS