The company's project has been in existence for two years, and the version is about 3. However, the local persistent data storage has always used GVUserDefaults, a third-party library that extends NSUserDefaults. However, with the development of the business, more and more places need to be stored, and GVUserDefaults can no longer meet the needs. When we couldn't stand it anymore, after some discussion, we decided to use FMDB, a third-party library that encapsulates SQLite3. This article takes this as the main line and sorts out some knowledge about databases and local storage, but this article does not mention CoreData at all. Students who like to use CoreData, I would like to say sorry for wasting your precious 20 seconds of time. The logic of this article is shown in the following figure: Overview of iOS local persistent storage Speaking of iOS local storage methods, everyone is probably familiar with them. NSUserDefault, File, Keychain, and DataBase are nothing more than these methods. NSUserDefault, File: Both of these methods are very simple to use. One thing that needs to be noted is that the stored objects need to comply with and implement the two methods in the NSCoding protocol. The applicable scope is also some small-scale data. In fact, the underlying implementation of NSUserDefault is still stored in .plist files. Basic knowledge of database First, there must be data Data, and then as the data increases, the name is upgraded to database DataBase. At this time, a management system is needed to manage the database, which is DataBaseManagerSystem. Finally, with the help of DataBaseAdministrator and the above names, it becomes a system, the so-called DataBaseSystem.
Data: The basic object stored in the database. SQL Statements SQL (Structured Query Language) is a language used to communicate with databases. Since you want to operate a database, you must be able to write SQL statements. Below I will briefly list a few simple SQL statements for reference. If you want to learn more about SQL statements, please refer to other materials. This article will not go into details here. Here we take the student table as an example to write a few simple SQL statements
Use of database If you don't use CoreData, whether you use the pure C language SQLite3 library or the encapsulated FMDB, you have to design a table structure that suits your business. There are two design patterns for table design. Using each attribute in the model as a field in the table makes it easier to query and read data, but I personally feel that this mode is suitable for businesses with a small number of records. And try not to change the fields as much as possible. After creating a table, it is still troublesome to modify the table structure. For specific usage, please refer to the following code:
The second design method This design pattern is to use the model as a field and directly convert the model to NSData and store it in this field. It should be pointed out here that the model must implement the NSCoding protocol. However, in actual projects, we mostly use third-party libraries to convert dictionaries to models. The three more popular third-party libraries for converting dictionaries to models, Mantle, MJExtension, and YYModel, all handle this by default, so just pay a little attention here. The second method is more suitable for a large number of records, and the relevance to the business is not too sensitive. If there is a need for search and sorting, some attributes can be selected from the model as some fields attached to the table. For specific use, refer to the following code:
Thread safety in database use When operating a database in multiple threads, we must consider thread safety issues, otherwise data may be confused. There are many solutions. You can lock it yourself, but if the read and write speed requirements are high, it is not recommended to lock it. SQLite3 directly supports multi-threading. The SQLite3 library provides three methods: single thread, multi thread, and serialized. FMDB also provides the FMDatabaseQueue class for multi-threaded database operation, which is relatively simple to use. Most apps now have local storage, but not every app has a corresponding cache deletion function. At least our own app does not have this function. Caching is to save traffic, and deleting cache is to save space. I think these two functions are equally important, but this requirement has been raised several times, but the product does not pay much attention to it! Especially in the 16G version, this function can also improve some user experience. This function is not difficult to implement. Just figure out the folders where the files are stored in various storage methods. Different data is obviously stored in different places. Then delete the corresponding data as needed. The specific directory of the sandbox is as follows:
When clearing the cache, you can delete all files in the Library/Caches folder, or you can delete a specified folder based on business needs. This article is more focused on the principles, and does not say too much about the specific uses. There are various specific uses, but the basic principles are relatively unified. As for database CURD, since we are not doing server-side development, we did not delve into it. If you want to study it, you can refer to other materials. In addition to the above, you may also need to consider database version migration, database synchronization and other requirements in specific use. Due to my limited level, I did not mention that you can refer to a reference blog post I wrote when I wrote this article, iOS Application Architecture Talks about Local Persistence Solutions and Dynamic Deployment. The usage of NSUserDefault and File is very simple and is not introduced. CoreData is too heavyweight, and I have never actually used it in my project, so it is not introduced here. (Copyright belongs to the author. Please contact the author for authorization if you want to reprint this article. The author has authorized this article to be reprinted.) |
<<: Agile development is good, but don’t follow it blindly
>>: iOS runtime practical application: member variables and properties
The dispute between brand advertising and perform...
When it comes to the most popular APP in 2019, Xi...
Law of Attraction Course + Wealth Attraction Cour...
In addition to allowing us to once again witness ...
After six years of exploration, the research team...
In the second half of the Internet , the user div...
Produced by: Science Popularization China Author:...
Landing page planning has always been a headache ...
The press conference on September 19, 2012 was th...
The fact that "Teacher Zhang"'s con...
Nowadays, many newcomers who are engaged in app p...
exe is our most common application installer, but ...
In 2017, the transaction volume on Double 11 reac...
What is cross-border marketing ? As American adve...