A Lightweight and Efficient Archiver Cache Framework Based on YYModel
development environment
Mac OS 10.12+ / Xcode 9+ / Objective-C
Supporting environment
iOS 8+, iPhone & iPad
Project acquisition
Here the code is shown by Objective-C and the project has been uploaded to github CBArchiver(https://github.com/cba023/YYModel-CBArchiver To use, import files to your project.
Function display
Instructions
Import project
Manual import
As shown in the figure, drag the "CBArchiver" folder into the project to be used in the framework, which can be used directly in the project.
function call
- Convert dictionary-type arrays to model-type arrays
NSArray * arr = dicJson[@"list"]; // arr: Dictionary array, self.list: Array of custom List type self.list = [arr convertToModelClass:[List class]];
- Save the data model locally
// flag: Storage Success BOOL flag = [CBArchiver.shared writeObj:self.list withFileName:DataName]; NSString * msg = flag == YES ? @"Write success" : @"Write fail";
- Data model saved before local reading
NSMutableArray <List *>* arrModel = [CBArchiver.shared readFromFileName:DataName];
- Data model saved before local deletion
// flag: YES for success, NO indicates that there is no such Archive BOOL flag = [CBArchiver.shared deleteFromFileName:DataName]; NSString * msg = flag == YES ? @" Delete success" : @"Delete fail, maybe file does not exist ";
Realization principle
Function encapsulation
- Get the path name of the archive file
/// Get data's name - (NSString *)getPathWithFileName:(NSString *)name { NSString * pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString * dataPath = [NSString stringWithFormat:@"%@/%@.bin",pathsToDocuments,name]; return dataPath; }
- Convert dictionary-type arrays to model-type arrays
- (NSMutableArray *)convertToModelClass:(Class)aClass { NSMutableArray * list = [NSMutableArray array]; for(id obj in self) { id model = [aClass yy_modelWithDictionary:obj]; [list addObject:model]; } return list; }
- Save the data model locally
// Write data to file - (BOOL)writeObj:(id)obj withFileName:(NSString *)name { NSString * p = [self getPathWithFileName:name]; BOOL flag = [NSKeyedArchiver archiveRootObject:obj toFile:p]; return flag; }
- Data model saved before local reading
// Read data from file - (id)readFromFileName:(NSString *)name { NSString * p = [self getPathWithFileName:name]; return [NSKeyedUnarchiver unarchiveObjectWithFile:p]; }
- Data model saved before local deletion
// Delete file by input name - (BOOL)deleteFromFileName:(NSString *)name { NSString * p = [self getPathWithFileName:name]; NSFileManager * fManager = NSFileManager.defaultManager; BOOL isExist = [fManager fileExistsAtPath:p]; if (isExist == NO) { return NO; } BOOL flag = [fManager isDeletableFileAtPath:p]; NSError *error; @try { [fManager removeItemAtPath:p error:&error]; } @catch (NSException *exception) { return NO; } @finally { return flag; } }
Pro, do you think the filing operation is much simpler after reading the introduction above? Yes, by encapsulating, we can call archives in very concise situations to achieve fast file localization, to cache your pages, or to store other lightweight data without understanding? Please forgive me for not writing beautifully. Welcome to github to download it. https://github.com/cba023/YYModel-CBArchiver.
Honey, have you learned? Hurry up and go to Hepi!
To readers
The project has been uploaded to github YYModel-CBArchiver(https://github.com/cba023/YYModel-CBArchiver You can directly star t or fork the project there. It may help you to develop the program efficiently for a long time. Of course, you are welcome to leave a message. There are some shortcomings or mistakes that can be corrected at any time. Your guidance and suggestions are the new driving force on my way forward.