The CoreData of ios-senior-data processing

Source: Internet
Author: User

Advantages of the 1.CoreData database framework

The CoreData data Persistence Framework is part of the Cocoa API for the first time in the iOS5 version of the system, which allows data to be organized according to the entity-attribute-value model and persisted in the format of XML, binary files, or sqlite data files.

CoreData mainly provides object-relational mapping (ORM) functions, converting OC objects to data saved to files, or data into OC objects.

SQLite:

1. Based on C interface, need to use SQL statement, code cumbersome

2. Table relationships are more intuitive when dealing with large amounts of data

3. Not visible in OC

CoreData:

1. Visualization, with Undo/redo capability

2. Multiple file formats can be implemented Nssqlitestoretype,nsxmlstoretype

3. Apple's official API support, more tightly integrated with iOS

Core objects:

Entity Management class: Nsmanagedobject

Entity Description Class: Nsentitydescription (menu detail Step)

Function: Used to describe an entity class

Data Manager class: Nsmanagedobjectcontext (Miss Waiter)

Role: Responsible for interaction between the application and the database

Data Connector class: Nspersistentstorecoordinator (Chef)

Role: Add a persistent repository

Data Conditioner class: Nspersistentstore (Kitchen)

Data Model class: Nsmanagedobjectmodel (menu)

Function: A model file representing CoreData

Core objects of the 2.CoreData database framework

To persist the storage and storage of files:

nspersistentstore: Persistent storage is a object-c representation of the actual file, a encapsulated underlying class for storing data.

Storage files: Files used to store and manage data, iOS supports 4 types of storage: Nssqitestoretype,nsbinarystoretype,nsinmemorystoretype,nsxmlstoretype

nsmanagedobjectcontext: Used to manipulate and consume data in the managed object context CoreData, which is responsible for the interaction between the application and the database.

Saving data requires Nsmangedobjectcontext for save operation

Data queries require nsmanagedobjectcontext for executefetchrequest operations (return values are arrays)

CoreData provides an object-relational mapping, and Nsmanagedobjectcontext operations are nsmanagedobject objects.

Nsmanagedobjectmodel: managed object model, managing multiple objects

nsmanagedobject: Managed objects, COREDATA returns the data model, the managed objects are generated according to the entity description.

nsentitydescription: Entity Description class, creating managed objects based on entity

entity class, which is a description of the file data. Managed objects represent entities, entities contain names, attributes (fields), and relationships, and the names of entities are usually consistent with the names of managed objects

nsfetchrequest: Query requests, you can do sort operations, or you can use predicates

Nsmanagedobjectcontext query data based on Nsfetchrequest, returned as an array, containing managed objects (Nsmanagedobject)

nssortdescriptor: Sorting operations

Simple operation of 3.CoreData database

1. Creating entity classes and attributes

To toggle a visualization diagram:

Part of the code:

@interface Viewcontroller ()

Declares a Managedobjectcontext object

@property (Nonatomic,strong) Nsmanagedobjectcontext *context;

@end

Initializes the context for data interaction between the application and the database

Self.context = [[Nsmanagedobjectcontext alloc] init];

Loading model files from applications

Nsurl *modelurl = [[NSBundle mainbundle] Urlforresource: @ "Data" withextension: @ "MOMD"];

(represents the entity that was just created)

Self.managedobjectmodel = [[Nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl];

Create a connector

The entity model associated with the connector object

................ (The above steps are available in the documentation) ... ..... ..... ............................

4.CoreData Database Table Association operations

5. Migrating using the CoreData database

CoreData supports the management of object model upgrades or modifications that evolve with app development, and changes to the model will result in incompatible (or not open) storage created by previous versions. If you want to change your model, you have to change the data in the existing store-the data storage format (store format)----This is called Data Migration (migration)

Three phases of data migration:

1. Create a target instance object based on the source instance object;

2. Re-establish contact

3. Verification and saving

Version Upgrade:

During the data migration process, CoreData created two stacks (stacks), one for the source store and one for the Target store (destination store). The coredata then gets the object from the source stack and inserts it into the target stack.

Note: In the new stack, many objects are coredata re-created (re-create)

Summarize:

The core of the implementation of the 1.CoreData database framework is the persistence of the storage stack

2. All interactions with the CoreData database framework are done through Nsmanagedobjectcontext
The data in 3.NSManagedObjectContext is cached in memory and must be updated to achieve persistence

The CoreData of ios-senior-data processing

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.