iOS data is persisted in the following ways: Plist file Write, Object archive, SQLite database, CoreData.
Plist file writes, object Archives are generally used for small amounts of data.
SQLite database, CoreData is used for large amount of data.
SQLite is a lightweight database, a relational database management system, designed to be used in embedded devices.
SQLite occupies a very low resource, ideal for use on mobile devices, and is open source free
SQLite database operation is actually the same as the normal database operation process:
1. Open Database Sqlite3_open ()
2. Preparing SQL statements, using parameter bindings to prevent SQL injection Sqlite3_bind_text () binding parameters
3. Compiling SQL statements SQLITE3_PREPARE_V2 ()
4, Execute SQL statement sqlite3_exec ()
5. End of statement sqlite3_finalize ()
6. Close the database Sqlite3_close ()
CoreData
CoreData is not a database, but rather a framework for storing data using databases, XML, binary files, and so on.
CoreData uses an object-oriented approach to manipulate data to address issues related to object lifecycle management, object graph management, and persistence.
CoreData avoids the complexity of SQL and allows us to interact with the database in a more natural way. The database is converted to OC objects (managed objects) to be implemented so that no SQL knowledge is required to manipulate them.
Pros: Good performance, the ability to design tables on Xcode, or to use instruments for performance testing, saves us the effort of coding, and better code quality than our own coding.
Features: Supports multiple types of external storage, undo/Redo, KVC, complex queries and object mappings, automatic validation, concurrency/merge policies, data migration, memory policies, and, in addition, a good combination of UI presentation.
Structure:
Nsmanagedobject: A model object, or entity, that can be directly understood as an OC object
Nsmanagedobjectmodel: Model files (Files ending in. xcdatamodeld). Record all Model objects (Nsentitydescription) and properties inside the object
Nspersistentstorecoordinator: Persistent storage coordinator. Dealing with databases to perform specific CRUD operations.
The Nsmanagedobjectcontext object management context. All persisted operations of the user are through this object. This object then calls the Nspersistentstorecoordinator object to perform the specific operation.
Correspondence Relationship:
Table Structure-Nsentitydescription
All the tables in the database and their contact Nsmanagedobjectmodel
Database Storage Method->nspersistentstorecoordinator
Database Operation->nsmanagedobjectcontext
Query Statement->nsfetchrequest
Record of the table->nsmanagedobject
Steps:
1. Read the template file: Nsmanagedobjectmodel describes the application data model and can contain multiple data model objects
2. Initialize the nspersistentstorecoordinator to perform a specific persistence operation. This tells the program how to store the data.
3. Initialize the Nsmanagedobjectcontext to perform the curd operation. Various ways to encapsulate a database
Database things mechanism
Thing mechanism: A sequence of operations performed by a single logical unit of work is called a database transaction, which prevents data inconsistencies when data updates occur synchronously. The simple point is that if all the data is submitted successfully and the data is submitted, the data is automatically rolled back, which is used more in the bank.
Properties of a transaction: atomicity, consistency, isolation, persistence
CoreData Version Migration: The data model has changed and we can achieve this by changing the version of the data model.
iOS Data Persistence mode analysis