Split entity of core data migration

Source: Internet
Author: User

From http://news.ipadown.com/14358

(ReferArticle: Http://blog.slalom.com/2010/01/08/splitting-an-entity-in-a-coredata-migration)

A major defect in the previous design of the Back-to-word software recently was to put the speech file of the word into the database and store it as a field in the Word Table. There was no major problem at the beginning of the test, but as the number of words increases, queries become very slow. Some functions added later must be frequently compared to the database. Therefore, an optimization is to specify the fields to be obtained in the core data fetch request. If the pronunciation field is excluded here, query is very fast, especially when I index the string of the word itself. HoweverCodeThat's ugly.

When we talk about Optimization in similar situations in core data, we need to create an entity for the big data file and establish a one-to-one relationship with the previous Entity. However, this document "core data model versioning and data migration programming" completely ignores how to split the entity content. Today I found an article to learn how to do it.

The following is a demonstration of the core data template generated based on xcode.ProgramAnd I suppose you already know the basic knowledge of model versioning and mapping object.
The original model is as follows:

Suppose our current goal is to separate the address into an independent entity.
Now create a new model version. Assume the name is version2 and make it like this:

In this way, the address is separated.
Next, we need to create a new mapping model file:

Set source to the original model file according to the wizard, and set target to the new model file. Then xcode generates the following mapping file:

On the basis of this file, we only need to make some modifications.
First, rename the address under Entity mapping to eventtoaddress (this step can be done or not), and then set source to event in Entity mapping on the right sidebar:

Next, change the value expression of address to $ source. Address as follows:

Switch to eventtoevent in the Entity mappings column on the left, select the address in relation mappings, set the Key Path to $ source in the right sidebar, and set the mapping name to eventtoaddress, use the name just now ).

Set the current version of the model to version2:

The last step is to initialize nspersistentstorecoordinator in the program, and add the nsmigratepersistentstoresautomaticallyoption option. The code is similar to the following:

1 _ persistentstorecoordinator = [[nspersistentstorecoordinator alloc] initwithmanagedobjectmodel: [self managedobjectmodel];
2 nsdictionary * Options = [nsdictionary dictionarywithobjectsandkeys:
3 [nsnumber numberwithbool: Yes], nsmigratepersistentstoresautomaticallyoption,
4 [nsnumber numberwithbool: Yes], nsinfermappingmodelautomaticallyoption, nil];
5 If (! [_ Persistentstorecoordinator addpersistentstorewithtype: nssqlitestoretype configuration: Nil URL: storeurl options: Options error: & error]) {
6 nslog (@ "unresolved error % @, % @", error, [error userinfo]);
7 Abort ();
8}

Finally, when the program is initialized, the migration will be automatically executed. If the amount of data to be migration is large, it will be very slow.
CompleteSource codeSee: https://gitcafe.com/hikui/iOS-Example/tree/master/CoreDataMigrationDemo

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.