IOS local persistent Storage

Source: Internet
Author: User
Tags key string

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.