Data Persistence storage mechanism in ios development

Source: Internet
Author: User

The persistent storage of data in IOS is similar to several common storage methods of files in Android.
For persistent data storage, ios generally provides four different mechanisms.
1. attribute list
2. Object Archiving
3. database storage (SQLite3)
4. Core Data, a persistence tool provided by Apple.


In fact, there are only a few storage formats, and we must also care about how these files will be placed under that file and then read.
That is to say, we need to know two things about data storage on IOS: the data storage format (that is, the storage mechanism) and the data storage location.
1. How to store files (such as the above four points)
2. Where is the file stored.
For data operations, we are actually concerned about the operation speed.
It is like preference storage, database storage, and io Storage in Adnroid.
I roughly asked our new ios buddy about the attribute list and database that their training institutions basically talk about data operations, and common file storage (such as multimedia data such as audio and video graphs ).
I had to read the book first.


I. Application file directory
First, let's take a look at the location of ios data storage, because only by knowing the location path can we read data, and the data persistence mechanism is only for consideration of the operation rate,
For example, we generally know that the storage volumes of the attribute list (in the form of key-value pairs) should be higher than the io file stream storage in the database.
The mechanism we choose to use to store data is mainly the form of Data Reading.


After an ios app is installed, the following folders and their corresponding paths are displayed:


View the application path in the simulator on mac:
/Users/nono/Library/Application Support/iPhone Simulator/5.1/Applications/2D135859-1E80-4754-B36D-34A53C521DE3

You may not be able to find the Library directory in the home directory in the finder, because it seems to have been hidden (on my machine, you can see it on the terminal ).
The last thing that is similar to the serial number is that ios automatically generates a group of unique app identifiers for the app's home directory name.
It is shown below.
This document introduces these folders:
Document: The application stores its data in this folder, except for settings based on NSUserDefaults preferences.
Basically, some of the data we want to operate is stored in this folder.
TIPS: here we mention that folder allocation for ios systems is because ITunes is selectively aware of file backup during device synchronization.
For example, we can guess that the backup will not be performed under tmp.
For obtaining the directory path of the Document folder, the API provides the following method:
[Cpp] view plaincopy
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docPath = [paths objectAtIndex: 0];
Library: stores Preferences in the Preferences folder Based on NSUserDefault Preferences. In short, this folder is rarely operated.
This section is not described in the book. It is estimated that the primary part is skipped.
Tmp: The application temporarily stores files. When this is not required, the application deletes the file data under it.
This file also provides the directory retrieval method:

JavaCode

14 .}

16. # pragma NSCopying protocol implementation

17.-(id) copyWithZone :( NSZone *) zone

18 .{

23 .}

24. @ end



Then, read and write the object archive.
[Cpp] view plaincopy
// Read the archive file
NSData * data = [[NSMutableDataalloc] initWithContentsOfFile: myFile];
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData: data];
TestObj * test = [unarchiver decodeObjectForKey: @ "data"];
[Unarchiver finishDecoding];
[Data release];
[Unarchiver release];

// Write the archive file
NSMutableData * data1 = [[NSMutableDataalloc] init];
NSKeyedArchiver * archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData: data1];
[Archiver encodeObject: test forKey: @ "data"];
[Archiver finishEncoding];
[Data writeToFile: myFile atomically: YES];
[Data1 release];
[Archiver release];
[Test release];

However, I asked my new colleague, But it is said that this is rarely used, at least for the moment.
However, after reading this, I think this is the same as Parcelable in Android.
Thenima looks like


Iii. database storage
Like Android, the embedded database SQLite3 is also used in ios.
There are many examples on the Internet. I can see the opening of the database here,
Create, query, and insert database tables
[Cpp] view plaincopy
// Database operations
Sqlite3 * database;
// Const NSString * dbname = @ "mydb"
Int result;
// Open an existing database in the specified path. If no database exists, a new database is created.
Result = sqlite3_open ([myFile UTF8String], & database );
If (result! = SQLITE_ OK ){
Sqlite3_close (database );
}

// Create a db table
Char * errorMsg;
NSString * SQL _create_table = @ "CREATE TABLE IF NOT EXISTS NONOTABLE is omitted ~~~~~~~~~~~~~ ";
Int result1;
// Sqlite_exec runs any command against sqlite3 to execute updates, inserts, and deletes. To put it simply, this method does not need to return data (although we may obtain a status value .).
Result1 = sqlite3_exec (database, [SQL _create_table UTF8String], NULL, NULL, & errorMsg );

// Search and query operations
Int result2;
Sqlite3_stmt * statment;
NSString * SQL _selected = @ "query statement ";
Result2 = sqlite3_prepare_v2 (database, [SQL _selected UTF8String],-1, & statment, nil );
If (result2 = SQLITE_ OK ){
// One-step operation
While (sqlite3_step (statment) = SQLITE_ROW ){
Int row = sqlite3_column_int (statment, 0 );
Char * rpwData = sqlite3_column_text (statment, 1 );
}
Sqlite3_finalize (statment );
}


// Bind the variable, which is a variant of the insert operation. For example, I mentioned above that sqlite_exec can be used to perform the insert operation. The inserted content is directly written in the SQL code, however, considering the invalid symbols involved in Word seek and some serious Injection Vulnerabilities (such as quotation marks that have been heard before ).
NSString * SQL _bind = @ "insert into foo value (?,?) ";
Result2 = sqlite3_prepare_v2 (database, [SQL _selected UTF8String],-1, & statment, nil );
If (result2 = SQLITE_ OK ){
Sqlite3_bind_int (status, 1,235 );
Sqlite3_bind_text (statment, 2, "test",-1, nil );
Sqlite3_finalize (statment );
}
If (sqlite3_step (statment )! = SQLITE_DONE)
NSLog (@ "error ");
Sqlite3_finalize (statment );


Sqlite3_close (database );

For more information, you can use Baidu on your own. Because the database operation syntax is too weird, the book says it is basically based on c, and I have never learned c. A little upset ~

4. Core Data Storage Mechanism
In general, the basic feeling is to visualize and simplify object archiving.
This section contains a lot of content. There are also a wealth of online materials.
I will not introduce it for the moment.

Summary: in fact, for ios data storage, the most common and important thing to master is the attribute list and database, because the two are of a high attendance rate.
We will consider using the other two mechanisms when data storage obviously shows the advantages of storage.
You must master the attribute list and sqlite operation storage.

 

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.