Comparison between SQLite and CoreData databases: sqlitecoredata

Source: Internet
Author: User

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
  • CoreData. framework: iOS encapsulates access to the original SQLite database API. Using this framework to manage data cache and persistent data is much easier and easier than using SQL statements to operate SQLite databases.

  • 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, NSManagedObjectModel

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.

  • NSPersistentStoreCoordinator

The objects of this class are usually initialized using the NSManagedObjectModel object. This class abstracts different storage methods and most often uses NSSQLiteStoreType.

  • NSManagedObjectContext

The object of this class is initialized using the NSPersistentStoreCoordinator object. It contains some methods to add and delete NSManagedObject.

  • NSFetchRequest

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.