Lazy loading is called delayed loading, he has two advantages, first: When used to load, not the start of loading, for the CPU to save time to do other things, second: The record will determine whether the data is empty, if empty is empty to load, to avoid the repeated loading of data, the system may clean up the memory so that the array is empty, This ensures that the array is not empty
Sometimes the plist file is not a layer may be two or even multilayer, remember to build a small model, how to implement lazy loading, below a practical example for you to demonstrate how to implement lazy loading, see the idea of implementation, rather than code.
CZCar.h
#import <Foundation/Foundation.h>
@interface Czcar:nsobject
/**
* The name of the picture
*/
@property (nonatomic,copy) NSString *icon;
/**
* Represents the brand of the car
*/
@property (nonatomic,copy) NSString *name;
/**
* Instantiated Object methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
-(Instancetype) Initwithdict: (Nsdictionary *) dict;
/**
* Instantiated class methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
+ (Instancetype) carwithdict: (Nsdictionary *) dict;
/**
* Class method, which returns an array that holds the model object, based on the incoming plist file name
*
* @param filename to pass in a plist file
*
* @return The array that holds the model object
*/
+ (Nsarray *) Carwithplistname: (NSString *) fileName;
@end
Czcar.m
#import "CZCar.h"
@implementation Czcar
/**
* Instantiated Object methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
-(Instancetype) Initwithdict: (Nsdictionary *) dict{
1. Initialization
self = [super init];
2. If self exists, the data is loaded
if (self) {
[Self setvaluesforkeyswithdictionary:dict];
}
3. Return object
return self;
}
/**
* Instantiated class methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
+ (Instancetype) carwithdict: (Nsdictionary *) dict{
return [[Self alloc]initwithdict:dict];
}
/**
* Class method, which returns an array that holds the model object, based on the incoming plist file name
*
* @param filename to pass in a plist file
*
* @return The array that holds the model object
*/
+ (Nsarray *) Carwithplistname: (NSString *) filename{
1. Loading data
Nsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle]pathforresource:filename ofType:@ "plist"]];
2. Dictionary Turn model
2.1 Create a temporary mutable array to hold the model object
Nsmutablearray *nmarray = [Nsmutablearray array];
2.2 Iterating through a dictionary array
For (nsdictionary *dict in array) {
2.3 Dictionary to model, add to variable array at the same time
[Nmarray addobject:[self carwithdict:dict];
}
2.4 Returns a mutable array stored as a model object
return nmarray;
}
@end
CZCarGroup.h
#import <Foundation/Foundation.h>
@interface Czcargroup:nsobject
/**
* An array of cars
*/
@property (Nonatomic,strong) Nsarray *cars;
/**
* Index Title
*/
@property (nonatomic,copy) NSString *title;
/**
* Instantiated Object methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
-(Instancetype) Initwithdict: (Nsdictionary *) dict;
/**
* Instantiated class methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
+ (Instancetype) cargroupwithdict: (Nsdictionary *) dict;
/**
* Class method, which returns an array that holds the model object, based on the incoming plist file name
*
* @param filename to pass in a plist file
*
* @return The array that holds the model object
*/
+ (Nsarray *) Cargroupwithplistname: (NSString *) fileName;
@end
"CZCARGROUP.M"
#import "CZCarGroup.h"
#import "CZCar.h"
@implementation Czcargroup
-(void) Setcars: (Nsarray *) cars{
_cars = cars;
The _cars store is the dictionary data
Dictionary Turn model
1. To create a mutable array
Nsmutablearray *nmarray = [Nsmutablearray array];
2. Iterating through a dictionary array
For (Nsdictionary *dict in cars) {
3. Dictionary Turn model
Czcar *car = [Czcar carwithdict:dict];
4. To add a model's object to a mutable array
[Nmarray Addobject:car];
}
5. Assigns the array of the model to the dictionary array
_cars = Nmarray;
NSLog (@ "Set data for Cars");
}
/**
* Instantiated Object methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
-(Instancetype) Initwithdict: (Nsdictionary *) dict{
1. Initialization
self = [super init];
2. If self exists, the data is loaded
if (self) {
Here, the dictionary-to-model is completed by KVC
[Self setvaluesforkeyswithdictionary:dict];
[Self setvalue:dict[@ "title"] forkeypath:@ "title"];
[Self setvalue:dict[@ "cars"] forkeypath:@ "cars"];
//
Self.cars = dict[@ "Cars"];
//
[Self setcars:dict[@ "cars"];
}
3. Return object
return self;
}
/**
* Instantiated class methods
*
* @param dict to pass in a dictionary data
*
* @return Returns the currently instantiated object
*/
+ (Instancetype) cargroupwithdict: (Nsdictionary *) dict{
return [[Self alloc]initwithdict:dict];
}
/**
* Class method, which returns an array that holds the model object, based on the incoming plist file name
*
* @param filename to pass in a plist file
*
* @return The array that holds the model object
*/
+ (Nsarray *) Cargroupwithplistname: (NSString *) filename{
1. Loading data
Nsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle]pathforresource:filename ofType:@ "plist"]];
2. Dictionary Turn model
2.1 Create a temporary mutable array to hold the model object
Nsmutablearray *nmarray = [Nsmutablearray array];
2.2 Iterating through a dictionary array
For (nsdictionary *dict in array) {
2.3 Dictionary to model, add to variable array at the same time
[Nmarray addobject:[self cargroupwithdict:dict];
}
2.4 Returns a mutable array stored as a model object
return nmarray;
}
@end
//
Viewcontroller.m
#import "ViewController.h"
#import "CZCarGroup.h"
#import "CZCar.h"
@interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate>
@property (Nonatomic,strong) Nsarray *cargrouparray;
@end
@implementation Viewcontroller
Override Cargrouparray's Get method to implement lazy loading-(Nsarray *) cargrouparray{
Determine if the data is nil
if (_cargrouparray = = nil) {
Loading data
_cargrouparray = [Czcargroup cargroupwithplistname:@ "Cars_total"];
}
return _cargrouparray;
}
Lazy load of thought and implementation