An approach to IOS on local persistent storage

Source: Internet
Author: User

At present, there are many ways to localize storage, and the following are commonly used:

1. Temporary cache

Let's start with the temporary cache, the temporary cache is typically used to manage some of the things that are commonly used globally in your application. such as the current user ID or the current location information.

The common way is to write a static variable and then use the class method call. (or a singleton class is OK)

Static Nsmutabledictionary *_cachedic;
#pragmaMark-Temporary cache (disappears after exiting the app) + (ID) Cachevalueforkey: (sccachekey) key{return[Self cachedic] valueforkey:[nsstring stringWithFormat:@"%d", Key]];}+(void) Setcachevalue: (ID) value Forkey: (sccachekey) key{[[Self cachedic] setobject:value forkey:[nsstring stringWithFormat:@"%d", Key]];}+(void) Deletecacheforkey: (Sccachekey) key{[[Self cachedic] removeobjectforkey:[nsstring stringWithFormat:@"%d", Key]];}

2. Local storage nsuserdefaults, by KV storage, will still exist after reboot. Disadvantage 1: can only store nsstring content, disadvantage 2: Delete the app will disappear

#pragma mark-field persistent cache (saved in 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) to store custom objects. Cons: Deleting apps will disappear.

#pragmaMark-Object Persistent cache (saved in local file) + (void) Setstoreobject: (nsobject<nscoding> *) obj Forkey: (scobjectkey) key{nsstring*path=[scsysconfig filepathbyname:[nsstring stringWithFormat:@"%d.domain", Key]]; [Nskeyedarchiver archiverootobject:obj Tofile:path];}+ (nsobject<nscoding> *) Storeobjectforkey: (scobjectkey) key{nsstring*path=[scsysconfig filepathbyname:[nsstring stringWithFormat:@"%d.domain", Key]]; NSObject<NSCoding> *obj=[Nskeyedunarchiver Unarchiveobjectwithfile:path]; returnobj;}+(void) Deletestoreobjectforkey: (scobjectkey) key{nsstring*path=[scsysconfig filepathbyname:[nsstring stringWithFormat:@"%d.domain", Key]]; [Scfileoper Removefile:path];}

Nskeyedarchiver the use of the method can refer to this blog: http://www.cnblogs.com/xiaobaizhu/p/4011332.html

4.KeyChain of use, after removing the app, reinstall still exists. (as long as Boundleid consistent)

A keychain is a keychain, which is a way to save data after the application has been deleted. It is related to the Boundleid of storage and application. For example, Baidu Bar is implemented in the application after the deletion, and then reinstall, if the token is still valid, no more landing.

Keychain use the official interface slightly annoying, generally use a third-party library, specifically, you can refer to my blog: http://www.cnblogs.com/rayshen/p/4671477.html

Demo:https://github.com/rayshen/localdatademo

An approach to IOS on local persistent storage

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.