IOS local persistent Storage
Currently, there are many methods for local storage, which are commonly used in the following ways: 1. The temporary cache refers to the temporary cache. The temporary cache is generally used to manage the content that needs to be used globally in applications. For example, the ID of the current user or the current location information. A common method is to write a static variable and call it using the class method. (Or a singleton class) static NSMutableDictionary * _ cacheDic;
# Pragma mark-field persistent cache (stored in the database) + (NSString *) storeValueForKey :( SCStoreKey) key {return [[NSUserDefaults standardUserDefaults] valueForKey: [@ (key) stringValue];} + (void) setStoreValue :( NSString *) value forKey :( SCStoreKey) key {[[NSUserDefaults standardUserDefaults] setObject: value forKey: [@ (key) stringValue]; [[NSUserDefaults standardUserDefaults] synchronize];} + (void) deleteStoreValueForKey :( SCStoreKey) key {[[NSUserDefaults standardUserDefaults] removeObjectForKey: [@ (key) stringValue]; [[NSUserDefaults standardUserDefaults] synchronize];}
2. Local Storage NSUserdefaults, stored by KV, And will exist after restart. Disadvantage 1: Only NSString content can be stored; disadvantage 2: The content disappears after the APP is deleted
# Pragma mark-field persistent cache (stored in the database) + (NSString *) storeValueForKey :( SCStoreKey) key {return [[NSUserDefaults standardUserDefaults] valueForKey: [@ (key) stringValue];} + (void) setStoreValue :( NSString *) value forKey :( SCStoreKey) key {[[NSUserDefaults standardUserDefaults] setObject: value forKey: [@ (key) stringValue]; [[NSUserDefaults standardUserDefaults] synchronize];} + (void) deleteStoreValueForKey :( SCStoreKey) key {[[NSUserDefaults standardUserDefaults] removeObjectForKey: [@ (key) stringValue]; [[NSUserDefaults standardUserDefaults] synchronize];}
3. NSKeyedArchiver (archive), which can store custom objects. Disadvantage: the APP will disappear after it is deleted.
# Pragma mark-object persistent cache (stored in a local file) + (void) setStoreObject :( NSObject <NSCoding> *) obj forKey :( SCObjectKey) key {NSString * path = [SCSysconfig filePathByName: [NSString stringWithFormat: @ "% d. domain ", key]; [abstrarchiverootobject: obj toFile: path];} + (NSObject <NSCoding> *) storeObjectForKey :( SCObjectKey) key {NSString * path = [SCSysconfig filePathByName: [NSString stringWithFormat: @ "% d. domain ", key]; NSObject <NSCoding> * obj = [NSKeyedUnarchiver unarchiveObjectWithFile: path]; return obj;} + (void) deleteStoreObjectForKey :( SCObjectKey) key {NSString * path = [SCSysconfig filePathByName: [NSString stringWithFormat: @ "% d. domain ", key]; [scfile1_removefile: path];}
4. Use of the KeyChain. After the APP is deleted, the re-installation still exists. (As long as the BoundleID is consistent) Keychain is a key string, which is used to save data after the application is deleted. Its storage is related to the BoundleID of the application. For example, Baidu Post Bar implements the re-installation after the application is deleted. If the Token is still valid, you do not need to log on again.