iOS has four common data storage methods:
First method: Store configuration information with Nsuserdefaults
Nsuserdefaults is designed to store configuration information for devices and applications, and it returns the default, and most commonly used, instance objects through a factory method. This object stores the configuration information of the user in the system, which can be modified by the instance object, or the new configuration items can be created according to their own requirements. He has not seen it in the file sandbox after IOS6, so you can store confidential information (user name and password) password to prevent the phone system from being cracked and look at your sandbox file. Generally this method stores a small amount of information, if you store a lot of information, such as storing 100 of data how do you record key values ah? So it is not conducive to unified management. So it is not recommended to use.
+ (void) Setuserdefaultsvalue: (ID) value key: (NSString *) key
{
Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
[userDefaults setObject:value forKey:key];[[NSUserDefaults standardUserDefaults] synchronize];
}
- (ID) Objectforkey: (NSString *) key
{
return [[Nsuserdefaults standarduserdefaults] objectforkey:key];
}
Call:
NSString *selectedkey = [[Nsuserdefaults standarduserdefaults] objectforkey:@ "Selectedkey"];
The second method: using CoreData to plan the objects in the application
An automated management solution that supports persistence, object graphs, and lifecycles. Strictly speaking, CoreData is a management solution, and his persistence can be stored through SQLite, XML, or binary files. As the official definition says, the role of CoreData is far more than storing data so simple that it can model and automate the management of objects throughout the application. He and Microsoft's Mfc::carchive implementation of object persistence and anti-persistence only supports support for functions with serialization, decomposing objects into types of basic data types, such as strings, shaping numbers, floating-point data, characters. Because persisted object data is in one object, he facilitates data management. Therefore, using CoreData to store data does not require the use of NSUSERDEFAULTS data to store data.
Third method: File sandbox storage
The main storage of non-confidential data, large data. such as: Download the picture, no key value of the branch record. Note that if the system is cracked, the files in your sandbox can be obtained by others. This method is simple to operate.
Write a file
+ (void) Writetodocumentwithimagedata: (nsdata ) data name: (NSString ) fileName
{
NSString *path = [self filepath:filename];
[data writeToFile:path atomically:YES];
}
Get file path
+ (nsstring*) FilePath: (nsstring*) FileName {
nsarray* mypaths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
nsstring* Mydocpath = [Mypaths objectatindex:0];
nsstring* FilePath = [Mydocpath stringbyappendingpathcomponent:filename];
return filePath;
}
Fourth method: Database storage
In the iOS SDK, the SQLite library is provisioned, and developers can build their own SQLite database. Each time SQLite writes data, it generates IO consumption and archives the data to the appropriate file.
SQLite is good at dealing with data types that are similar to nsuserdefaults and are basic types of small data, just different from the organization. Developers can organize data in a relational database, using SQL DML to manage the data. Generally speaking, the formatted text class data in the application can be stored in the database, especially the data such as chat record, timeline and so on which have conditional query and sorting requirements.
Regardless of whether you take the system or use the Sqlight third-party library of the data storage nature of the database storage, there is no need to separate classification. Data storage is a little cumbersome, and storage is slow, only the real need to use the place in this way, such as: Chat records, map geographic information query.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Four ways to store and tune your iOS data