Comparison between SQLite and CoreData databases: sqlitecoredata
1. SQLite Database
The basic process for operating the sqlite database is to create a database and define the table structure by defining some fields. You can use SQL statements to insert records into the table, delete records, and modify records, you can also establish a connection between tables.
These operations must be implemented using SQL statements, and the operation is direct. If there is a bit of database and SQL basics Before, it will feel very friendly to write, are some database operation statements. However, when the number of operations increases, the number of statements increases, which is annoying, and the code is too large to appear confusing.
- For more information, see the SQLite database for processing data in iOS learning 36.
2. CoreData Database
Mechanism: you only need to use the classes provided by the framework to store and query data through the framework. You can use the object to manage your data and the relationship between the data, framework has encapsulated database tables and fields into objects and attributes. The one-to-many and many-to-many relationships between tables are encapsulated into the inclusion relationships between objects.
Expressed by relationship:
One-To-Many: Select Optional and Plural as-required Relationship for the properties item;
Multiple-to-one inverse relationship: Optional is selected for the properties item, Minimun and Maximun are selected for the Count item, and their values are set to 1 respectively;
If you want to learn more, you can refer to the iOS learning 37 Data Processing CoreData
3. Comparison
1> the CoreData class corresponds to the text description in the SQLite database:
NSEntityDescription --> table structure
NSManagedObjectModel --> contact all tables in the database
NSPersistentStoreCoordinator --> database storage method
NSManagedObjectContext --> database operations
NSFetchRequest --> query statement
NSManagedObject --> Table Record
2> let's take a detailed look at the CoreData class.
NSEntityDescription is used to define the table structure, so you can understand the setEntities in NSManagedObjectModel: (NSArray *) What is the use of the entities function. generally, the model is defined using the file CoreData. xcda ***** odel, graphical operation. this is similar to using nib to create an interface.
The objects of this class are usually initialized using the NSManagedObjectModel object. This class abstracts different storage methods and most often uses NSSQLiteStoreType.
The object of this class is initialized using the NSPersistentStoreCoordinator object. It contains some methods to add and delete NSManagedObject.
Generally, NSEntityDescription is used to construct a query, which specifies the table to be queried and the sorting can be specified.
- NSFetchedResultsController
The NSFetchedResultsController only closed it again and used it together with NSFetchRequest to facilitate data retrieval. In addition, it is associated with NSManagedObjectContext to receive a notification when the database changes.
3> comparison of features
The strength of CoreData is that when an object is updated, its associated objects will also be updated, which is equivalent to when you update a table, other associated tables will also be updated.
Another feature of Core Data is to provide a simpler performance management mechanism. For example, if you use the NSFetchedResultsController class, you do not need to use the SQL Limit but use the setFetchBatchSize () of the NSFetchRequest class () you can limit the total number of query records. The magic of the NSFetchedResultsController class is that when you need to obtain more records, this class will automatically update its cache.
For multi-table queries, CoreData is not intuitive in SQL, but CoreData can still perform related operations. However, for operations such as external connections and left connections, it seems powerless in CoreData.