One of the most common interview questions is, what is iOS data persistence? And its advantages and disadvantages?

In this paper, the content
  • Several types of iOS local data persistence;
  • Application scenarios

1. Type of iOS local data persistence

  • 1.1 NSUserDefaults
  • 1.2 plist
  • 1.3 Keychain
  • 1.4 Archiving (Unarchiving)
  • 1.5 sandbox
  • 1.6 database
  • 1.7 CoreData

2. Various application scenarios

2.1 NSUserDefaults

Used to store user preferences and user information, such as user name, automatic login, font size, etc. The data is automatically saved in the Libarary/Preferences directory of the sandbox.

It can store the following data types :NSNumber (NSInteger, float, double), NSString, NSDate, NSArray, NSDictionary, BOOL.

Storage of custom objects is not supported.

Advantages: Easy to use disadvantages: NSUserDefaults stores input data in. Plist format files, which determines that its security is almost zero, so it is not recommended to store some sensitive information such as: user passwords, tokens, encryption private keys, etc. = =

Issues needing attention: ==NSUserDefaults stores immutable data, so if you want to store mutable data, you have to be immutable to store it. ==. NSUserDefaults writes data from the cache to disk at a certain time, not immediately, To prevent data loss caused by program exit after writing NSUserDefaults, you can use Synchronize to force data to be written to disk immediately after writing data.

2.2 plist

The Property List file, the full name is Property List, the extension of this file is.plist, so it’s usually called a plist file. It is a file used to store serialized objects. It is used to store frequently used data in programs with small amount of data that is not often changed.

Types that can be stored :NSNumber, NSString, NSDate, NSData,NSArray, NSDictionary, BOOL. Storage of custom objects is not supported.

The advantages and disadvantages:

  • Ease of use is strong
  • Used to store small amounts of data, not suitable for handling large amounts of data
  • Unable to serialize custom objects into properties; If you want to store custom types of data need to be serialized first!

2.3 keychain

It is used to store important data locally. After data is encrypted, it is more secure to store data locally. Such as: password, secret key, serial number and so on.

After you delete the APP, the data stored in the Keychain will not be deleted. Therefore, the data in the Keychain can still be used after the APP is reinstalled. Starting with ios 3.0, it’s possible to share keychain across applications and NSUserDefaults stores data that will be deleted with the APP.

The file KeychainItemWrapper has been wrapped by Apple to use keychain. Of course, you can also use other excellent third-party encapsulation, such as ssKeychain. Usage: Check official documents or search baidu

2.4 archive (NSKeyedArchiver)

Archiving is a common technique of data storage in iOS development. == Archiving can directly store objects into files and read files into objects. = =

Generally used to store small amounts of data

Archives can store more diverse data types and access custom objects than plist or UserDefault forms. Objects archive files that are confidential and cannot be viewed on disk, which is more secure.

Comply with the NSCoding protocol and implement the two methods in the protocol. If it is inherited, the subclass must override those two methods. Because the subclass will look for the method in the subclass when accessing it, it will look for the method in the parent class if it doesn’t find it, so the new attributes will be ignored when it finally saves and reads. We need to call the methods of the parent class first, initialize the parent class first, and then initialize the child class.

The file name extension that holds the data can be named at will

The biggest advantage is that complex objects can be written to files and archived collection classes, so no matter how many objects are added, the way objects are written to disk is the same without increasing the workload;

2.5. The sand box

Persisted in the Document directory, generally storing unclassified data. Sandbox storage is recommended when the App involves reading e-books, listening to music, watching videos, scrolling through picture lists, etc. Because this can greatly save user traffic, but also enhance the app experience effect.

Application: Stores Application source files, which must be digitally signed before being uploaded and cannot be modified after being uploaded.

Documents: Save persistent data generated when running. This directory is backed up when iTunes synchronizes the device. For example, a game application can save a game archive in this directory.

TMP: saves the temporary data required for operation. After the operation is complete, the corresponding files are deleted from the directory. If the application is not running, the system may delete files in this directory. ITunes does not back up this directory when it syncs devices.

Library/Caches: Store persistent data from app runtime, which iTunes does not back up when syncing devices. Non-critical data that is large and does not need to be backed up, such as network data Caches, are stored under Caches

Library/Preference: Stores all of the app’s preferences. For example, Settings on iOS should look for Settings in this directory. This directory is backed up when iTunes syncs devices.

2.6 database

Suitable for storing large amounts of data, generally using FMDB and CoreData to achieve.

FMDB is the SQLite database framework of iOS platform. FMDB encapsulates THE SQLite C language API in the form of OC, which is more object-oriented and saves a lot of trouble and redundant C language code. Compared with Apple’s own Core Data framework, FMDB is more lightweight and flexible. Multithread safe database operation method is provided to effectively prevent data chaos.

The usability is not strong, but can store a large amount of data, storage and retrieval of a large amount of data is very efficient; The ability to perform complex aggregations of data is much more efficient than using objects.

2.6 CoreData

CoreData:

Core Data is a post-ios5 framework that provides object-relational mapping (ORM) capabilities that can convert OC objects to Data stored in SQLite database files and restore Data stored in databases to OC objects. We do not need to write any SQL statements during this data operation.

CoreData essentially means that the data is stored in an SQLite database file, which is not very convenient to use.

MagicRecord is the second encapsulation of CoreData, easy to use and easy to operate.

FMDB and MagicRecord have different performance aspects and need to be selected according to the actual needs of the project. There is no best plan only the most suitable plan!