iOS Data Persistence Solution

Origin of technology

Data persistence is an essential skill in iOS development. Because in development, we often involve one or several scenarios in user information storage , file storage , and application content caching .

Several ways of data persistence

  • NSUserDefaults
  • plist
  • keychain (keychain)
  • archive
  • Sandbox
  • database

An overview of several ways of data persistence:
data persistence

1.NSUserDefaults

NSUserDefaults is used to store user preferences and user information , such as user name, whether to log in automatically, font size, etc.
The data is automatically saved in the Libarary/Preferences directory of the sandbox
. NSUserDefaults stores the input data in a file in .plist format Under the circumstances, this storage method determines that its security is almost 0, so it is not recommended to store some sensitive information such as: user password, token, encrypted private key, etc. The data type it can store is: NSNumber (NSInteger,
float , double), NSString, NSDate, NSArray, NSDictionary, BOOL.
Storage of custom objects is not supported.

Points to note:

  1. The data stored in NSUserDefaults is immutable. If you want to store variable data in it, you need to convert it to immutable before you can store it .
  2. NSUserDefaults writes the data in the cache to the disk at regular intervals, rather than writing them immediately. In order to prevent data loss caused by the program exiting after writing NSUserDefaults, you can use synchronize to force the data to be written to the disk immediately after writing the data .

2.plist

That is, the property list file , the full name is Property List, and the extension of this file is .plist, so it is usually called a plist file. It is a file used to store serialized objects , and is used to store data that is often used in programs and has a small amount of data and does not change frequently.
Types that can be stored: NSNumber, NSString, NSDate, NSData, NSArray, NSDictionary, BOOL .
The storage of custom objects is not supported.

Points to note:
If you need to store custom types of data, you need to serialize it first!

3.Keychain (key chain)

For the storage of important local data , it is more secure to store the encrypted data locally. Such as: password, secret key, serial number, etc. When you delete the APP, the data stored in the Keychain will not be deleted, so after reinstalling the App, the Keychain The data in it can still be used. Starting from ios 3.0, it becomes feasible to share keychain across programs, and the data stored in NSUserDefaults will be deleted along with the APP.
When using keychain, Apple has officially packaged the file KeychainItemWrapper for us , and it can be used after importing it. Of course, other excellent A third-party package, such as ssKeychain .
How to use keychain

4. Archive (NSKeyedArchiver)

Archiving is a common technique for data storage in iOS development. Archiving can directly store objects into files and read files into objects .
Compared with plist or userdefault, archives can store more diverse data types, and can access custom objects. The files of the object archive are confidential , and the content in the files cannot be viewed on the disk, which is more secure.
Comply with the NSCoding protocol and implement the two methods in the protocol. If it is inheritance, the subclass must override those two methods. Because when the subclass is accessing, it will go to the subclass to find the calling method. If it does not find it, it will go to the parent class to find it, so the newly added attributes will be ignored when saving and reading. You need to call the method of the parent class first, initialize the parent class first, and then initialize the subclass.
The suffix of the file saving the data can be named freely .

storage type safety filename suffix data size Application Scenario
NSUserDefaults unsafe plist Small User Preferences, Username
plist unsafe plist Small infrequently changed
keychain Safety Small Password, secret key, serial number
archive Safety arbitrarily big cache

5. Sandbox

Persist in the Document directory, generally storing non-confidential data . When the app involves reading e-books, listening to music, watching videos, swiping picture lists, etc., it is recommended to use sandbox storage. Because this can greatly save user traffic, and also enhance the experience of the app.

Application: Stores program source files, which are digitally signed before being put on the shelf, and cannot be modified after being put on the shelf .

Documents: Save the data that needs to be persisted when it should run, and it will back up this directory when iTunes synchronizes the device . For example, game applications can save game saves in this directory.

tmp: Save the temporary data needed for running, and delete the corresponding files from this directory after use . When the application is not running, the system may also clear the files in this directory. This directory is not backed up when iTunes syncs the device .

Library/Caches: Save the data that needs to be persisted when the application is running, and this directory will not be backed up when iTunes synchronizes the device . Generally, non-important data that is large in size and does not need to be backed up, such as network data caches are stored under Caches.

Library/Preference: Save all the preferences of the application , such as iOS Settings (Settings) should look for the application's setting information in this directory. This directory is backed up when iTunes syncs the device .

6. Database

To store data with a large amount of data, it is generally stored in a database. Such as: FMDB , CoreData , Realm , WCDB .

6.1 FMDB

FMDB is the SQLite database framework of the iOS platform. FMDB encapsulates SQLite's C language API in the form of OC, which is more object-oriented to use and saves a lot of trouble and redundant C language code . Compared with Apple's own Core Data framework, It is more lightweight and flexible, and provides a multi-thread safe database operation method to effectively prevent data confusion.

6.2 CoreData

Core Data is a framework that appeared after iOS5. It provides the function of object-relational mapping (ORM) , that is, it can convert OC objects into data, save them in SQLite database files, and restore the data stored in the database. into OC objects. During this data operation, we don't need to write any SQL statements. But it is not so easy to directly operate CoreData, so I will use MagicRecord to achieve it most of the time. MagicRecord is a secondary package of CoreData, which is easy to use and easy to operate.

6.3 Realm

Realm 's GitHub Portal using Realm

6.4 WCDB

WCDB is an open source database component of WeChat mobile terminal.
Introduction to the use of WCDB
Detailed introduction to
WCDB GitHub Portal of WCDB

write at the end

This article is mainly a summary of the data persistence solutions used in the iOS development process . Some of them have not been used in detail by myself, but the corresponding links are attached, and students in need can learn in detail.

Reference: http://www.cocoachina.com/ios/20180530/23575.html?utm_source=tuicool&utm_medium=referral

Guess you like

Origin blog.csdn.net/u010389309/article/details/88369088