SoFunction
Updated on 2025-04-12

Lazy loading of iOS usage instance code

Write in front

When I was configuring array data, the code I wrote last night suddenly thought that when I loaded the data, I didn’t have to load it in viewDidLoad, and when it can be loaded. This technique is the lazy loading method of this article. Then record it. The content is as follows:

What is lazy loading

Lazy loading is also called lazy loading. It means that the system will not load an object when initializing it, but will only load the object into memory when it is called (using the get method). To put it simply, it will load it when the object is needed by the system or developer. Its implementation method is essentially overriding the get method of the object and implementing the code that the object needs to be implemented during initialization in the get method.

Advantages of using lazy loading

  1. There is no need to instantiate objects in viewDidLoad, simplify code, make the structure clear and easy to understand, and enhance the readability of the code.
  2. Instance of objects In getter methods, objects perform their own functions and reduce code coupling.
  3. Improve initialization loading speed, and reduce the memory usage of the system
  4. Reduce memory usage

viewDidLoad normal loading code example

Take the array used in the implementation of yesterday's function as an example.viewDidLoadThe normal loading in the middle is like this:

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *infoArr;//Array@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [self configData];
}

- (void)configData {
  _infoArr = @[@{@"title":@"Tour date", @"routeName":@"Line Name One", @"time":@"2015/11/21", @"num":@"20", @"price":@"124.0", @"code":@"DAGSDSASA"},
         @{@"title":@"Liu Po", @"routeName":@"Line Name Two", @"time":@"2015/11/21", @"num":@"34", @"price":@"234", @"code":@"TAGDFASFAF"},
         @{@"title":@"price", @"routeName":@"Line Name Three", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"},
         @{@"title":@"Group Code", @"routeName":@"Line Name Four", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}];
}

It is easy to see from the above code snippet that when the controller is loaded, the content in viewDidLoad will start to load. Assuming that this _infoArr will be called after some events are triggered, there is no need to load the array data after the controller is loaded. If the event is not triggered, it means that _infoArr will never be loaded. In this way, loading it in viewDidLoad will appear to be a lot of extra memory and consume a lot of memory.

Lazy loading method example

- (NSArray *)infoArr {
  if (!_infoArr) {
    _infoArr = @[@{@"title":@"Tour date", @"routeName":@"Line Name One", @"time":@"2015/11/21", @"num":@"20", @"price":@"124.0", @"code":@"DAGSDSASA"},
           @{@"title":@"Liu Po", @"routeName":@"Line Name Two", @"time":@"2015/11/21", @"num":@"34", @"price":@"234", @"code":@"TAGDFASFAF"},
           @{@"title":@"price", @"routeName":@"Line Name Three", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"},
           @{@"title":@"Group Code", @"routeName":@"Line Name Four", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}];
  }
  return _infoArr;
}

The loading method of calling getter methods like the above is lazy loading. In this way, when _infoArr is needed, the [self infoArr] method will be called (it is also a getter method). At this time, the system will call the getter method, then get the data assignment in the getter method, and then return it for use (the point that needs to be noted is not to use it in the getter method, because the getter method will be called, causing a dead loop).

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.