Introduction The so-called persistence is to save data to the hard disk so that the previously saved data can be accessed after the application or machine is restarted. In iOS development, there are many data persistence solutions. Next, I will try to introduce 5 solutions:
Sandbox Before introducing various storage methods, it is necessary to explain the following sandbox mechanism. By default, iOS programs can only access their own directory, which is called the "sandbox". 1. Structure Since the sandbox is a folder, let's take a look at what's inside. The directory structure of the sandbox is as follows:
2. Directory Features Although there are so many folders in the sandbox, all folders are different and have their own characteristics. So when choosing a storage directory, you must carefully choose a suitable directory. "Application package": This contains the source files of the application, including resource files and executable files.
Documents: The most commonly used directory. When iTunes syncs the app, it will sync the contents of this folder. It is suitable for storing important data.
Library/Caches: iTunes will not synchronize this folder. It is suitable for storing large and non-important data that does not need to be backed up.
Library/Preferences: When iTunes syncs the app, it will sync the contents of this folder, which usually stores the app's settings. tmp: iTunes will not synchronize this folder. The system may delete the files in this directory when the application is not running. Therefore, this directory is suitable for storing some temporary files in the application and deleting them after use.
plist file The plist file saves certain specific classes in a directory in the form of XML files. The only types that can be serialized are:
1. Get the file path
2. Storage
3. Read
4. Note
Preference 1. How to use
2. Note
#p# NSKeyedArchiver Archiving is another form of serialization in iOS. Any object that follows the NSCoding protocol can be serialized through it. Since most Foundation and Cocoa Touch classes that support data storage follow the NSCoding protocol, archiving is relatively easy to implement for most classes. 1. Follow the NSCoding protocol The NSCoding protocol declares two methods, both of which must be implemented: one that describes how to encode an object into an archive, and the other that describes how to unarchive it to get a new object.
If the class to be archived is a subclass of a custom class, you need to implement the archiving and unarchiving methods of the parent class before archiving and unarchiving, that is, the [super encodeWithCoder:aCoder] and [super initWithCoder:aDecoder] methods; 2. Use To archive an object, call the NSKeyedArchiver factory method archiveRootObject: toFile: .
If you need to unarchive an object from a file, just call a factory method of NSKeyedUnarchiver, unarchiveObjectWithFile:.
3. Note
SQLite3 All previous storage methods are overwriting storage. If you want to add a piece of data, you must read the entire file, modify the data, and then overwrite the entire content into the file. Therefore, they are not suitable for storing large amounts of content. 1. Field Type On the surface, SQLite divides data into the following types:
In fact, SQLite is typeless. That is, no matter what field type you specify when creating a table, the storage can still store any type of data. And you don't have to specify the field type when creating a table. The reason why SQLite has types is for good programming standards and to facilitate communication between developers, so it is best to set the correct field type when using it! The primary key must be set to integer 2. Preparation The preparation work is to import the dependent library. To use SQLite3 in iOS, you need to add the library file: libsqlite3.dylib and import the main header file. This is a C language library, so it is quite troublesome to use SQLite3 directly. 3. Use
Before operating the database, you must first specify the database file and the table to be operated. Therefore, when using SQLite3, you must first open the database file and then specify or create a table.
The sqlite3_exec() method can be used to execute any SQL statement, such as table creation, update, insert, and delete operations. However, it is generally not used to execute query statements because it does not return the queried data.
As mentioned above, the sqlite3_exec() method is generally not used to query data. Because querying data requires obtaining query results, the query is relatively troublesome. The sample code is as follows:
4. Summary Generally speaking, the use of SQLite3 is still quite troublesome, because they are all C language functions, which are difficult to understand. However, in the general development process, the third-party open source library FMDB is used, which encapsulates these basic C language methods, making it easier to understand when we use it and improving development efficiency. #p# FMDB 1. Introduction FMDB is a SQLite database framework for iOS. It encapsulates the SQLite C language API in an OC way. Compared with the C language framework that comes with Cocoa, it has the following advantages: It is more object-oriented 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 Provides a multi-threaded safe database operation method to effectively prevent data confusion Note: FMDB's gitHub address 2. Core Classes FMDB has three main classes:
An FMDatabase object represents a single SQLite database and is used to execute SQL statements.
The result set after executing the query using FMDatabase
Used to execute multiple queries or updates in multiple threads, it is thread-safe 3. Open the database Like the C language framework, FMDB creates an FMDatabase object by specifying the SQLite database file path, but FMDB is easier to understand and use. Before using it, you still need to import sqlite3.dylib. The method to open the database is as follows:
It is worth noting that the value of Path can be passed in the following three situations:
4. Update In FMDB, all operations except query are called "updates", such as create, drop, insert, update, delete, etc., and the executeUpdate: method is used to perform updates:
5. Query There are also 3 query methods, which are quite simple to use:
Query example:
6. Thread Safety It is unwise to use an FMDatabase instance in multiple threads at the same time. Do not let multiple threads share the same FMDatabase instance, which cannot be used in multiple threads at the same time. If you use an FMDatabase instance in multiple threads at the same time, it will cause data confusion and other problems. Therefore, please use FMDatabaseQueue, which is thread-safe. Here is how to use it:
And you can easily wrap simple tasks into transactions:
FMDatabaseQueue will create a serialized GCD queue behind the scenes and execute the blocks you pass to the GCD queue. This means that even if you call the method from multiple threads at the same time, GDC will execute it in the order it receives the blocks. |
>>: The brand extension crisis of Microsoft, Xiaomi and LeTV
Achieve 100,000 users in 6 months I had just arri...
Author: Hu Rui, Unit: China Mobile Smart Home Ope...
1. Meizu advertiser registration issues 1. Under ...
1. Background How to measure and simulate "w...
[[397036]] According to foreign media The Informa...
After the content of Huaban.com was rectified A l...
I believe that everyone likes to watch movies and...
[[417832]] There are many accessibility features ...
What are the marketing methods of Tik Tok? What p...
Planning an event is not an easy task. It is esse...
As the Douyin live e-commerce ecosystem continues...
ByteDance denies Microsoft's bid for TikTok...
Course Description Many times, some inconspicuous...
For products, learning to do effective competitiv...
The decline of traffic dividends is now an irreve...