Thoughts and implementations of lazy loading and implementation of lazy loading
Lazy loading is also called delayed loading. It has two advantages: first, loading is used, instead of loading at the beginning, to save time for the CPU, and second: before loading, the system determines whether the data is empty. If the data is empty, the system will load the data to avoid repeated data loading. The system may clear the memory and leave the array empty so that the array is not empty.
Sometimes a plist file may not have two or more layers at a layer. Remember to first create a small model and how to implement lazy loading. The following example shows how to implement lazy loading, it looks at the implementation idea, not the code.
// CZCar. h
# Import <Foundation/Foundation. h>
@ Interface CZCar: NSObject
/**
* Image name
*/
@ Property (nonatomic, copy) NSString * icon;
/**
* Represents the automobile brand
*/
@ Property (nonatomic, copy) NSString * name;
/**
* Instantiated object Method
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
-(Instancetype) initWithDict :( NSDictionary *) dict;
/**
* Instantiated class methods
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
+ (Instancetype) carWithDict :( NSDictionary *) dict;
/**
* Class method: returns an array containing Model Objects Based on the input plist file name.
*
* @ Param fileName: input the file name of a plist file.
*
* @ Return: array of Model Objects
*/
+ (NSArray *) carwit2d-istname :( NSString *) fileName;
@ End
// CZCar. m
# Import "CZCar. h"
@ Implementation CZCar
/**
* Instantiated object Method
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
-(Instancetype) initWithDict :( NSDictionary *) dict {
// 1. Initialization
Self = [super init];
// 2. If self exists, load the data
If (self ){
[Self setValuesForKeysWithDictionary: dict];
}
// 3. Return object
Return self;
}
/**
* Instantiated class methods
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
+ (Instancetype) carWithDict :( NSDictionary *) dict {
Return [[self alloc] initWithDict: dict];
}
/**
* Class method: returns an array containing Model Objects Based on the input plist file name.
*
* @ Param fileName: input the file name of a plist file.
*
* @ Return: array of Model Objects
*/
+ (NSArray *) carwit2d-istname :( NSString *) fileName {
// 1. load data
NSArray * array = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: fileName ofType: @ "plist"];
// 2. dictionary-to-Model
// 2.1 create a temporary variable array to store Model Objects
NSMutableArray * nmArray = [NSMutableArray array];
// 2.2 traverse the dictionary Array
For (NSDictionary * dict in array ){
// 2.3 convert the dictionary to the model and add it to the variable array
[NmArray addObject: [self carWithDict: dict];
}
// 2.4 return a variable array that is stored as a model object
Return nmArray;
}
@ End
// CZCarGroup. h
# Import <Foundation/Foundation. h>
@ Interface CZCarGroup: NSObject
/**
* Car Array
*/
@ Property (nonatomic, strong) NSArray * cars;
/**
* Index title
*/
@ Property (nonatomic, copy) NSString * title;
/**
* Instantiated object Method
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
-(Instancetype) initWithDict :( NSDictionary *) dict;
/**
* Instantiated class methods
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
+ (Instancetype) carGroupWithDict :( NSDictionary *) dict;
/**
* Class method: returns an array containing Model Objects Based on the input plist file name.
*
* @ Param fileName: input the file name of a plist file.
*
* @ Return: array of Model Objects
*/
+ (NSArray *) cargroupwit2d-istname :( NSString *) fileName;
@ End
// "CZCarGroup. m"
# Import "CZCarGroup. h"
# Import "CZCar. h"
@ Implementation CZCarGroup
-(Void) setCars :( NSArray *) cars {
// _ Cars = cars;
// _ Cars stores Dictionary data.
// Dictionary-to-Model
// 1. Create a variable array
NSMutableArray * nmArray = [NSMutableArray array];
// 2. traverse the dictionary Array
For (NSDictionary * dict in cars ){
// 3. dictionary-to-Model
CZCar * car = [CZCar carWithDict: dict];
// 4. Add the model object to the variable array
[NmArray addObject: car];
}
// 5. assign an array containing the model to the dictionary Array
_ Cars = nmArray;
// NSLog (@ "Set Data for cars ");
}
/**
* Instantiated object Method
*
* @ Param dict input a dictionary data
*
* @ Return returns the current instantiated object
*/
-(Instancetype) initWithDict :( NSDictionary *) dict {
// 1. Initialization
Self = [super init];
// 2. If self exists, load the data
If (self ){
// Dictionary conversion model completed through 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 input a dictionary data
*
* @ Return returns the current instantiated object
*/
+ (Instancetype) carGroupWithDict :( NSDictionary *) dict {
Return [[self alloc] initWithDict: dict];
}
/**
* Class method: returns an array containing Model Objects Based on the input plist file name.
*
* @ Param fileName: input the file name of a plist file.
*
* @ Return: array of Model Objects
*/
+ (NSArray *) cargroupwit2d-istname :( NSString *) fileName {
// 1. load data
NSArray * array = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: fileName ofType: @ "plist"];
// 2. dictionary-to-Model
// 2.1 create a temporary variable array to store Model Objects
NSMutableArray * nmArray = [NSMutableArray array];
// 2.2 traverse the dictionary Array
For (NSDictionary * dict in array ){
// 2.3 convert the dictionary to the model and add it to the variable array
[NmArray addObject: [self carGroupWithDict: dict];
}
// 2.4 return a variable array that is 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
// Rewrite the get method of carGroupArray to implement lazy loading-(NSArray *) carGroupArray {
// Determine whether the data is Nil
If (_ carGroupArray = nil ){
// Load data
_ CarGroupArray = [CZCarGroup cargroupwit2d-istname: @ "cars_total"];
}
Return _ carGroupArray;
}