Core Data Migration 之拆分Entity

hikui posted @ 2012年12月19日 00:23 in iOS开发 , 3467 阅读

参考文章:http://blog.slalom.com/2010/01/08/splitting-an-entity-in-a-coredata-migration/

最近自己做的背单词软件,在之前的设计上有一个非常大的缺陷就是把单词读音的语音文件放在数据库里面了,而且作为word表里面的一个字段储存。一开始测试的时候没有什么大问题,但是单词越来越多之后查询就变得非常之慢。后面自己加上的一些功能都要频繁地对比数据库,所以做了一个优化就是在core data fetch request里面指定要获取的字段,在这里排除读音字段的话,查询就非常快了,尤其当我把单词本身的string做了index之后。但是代码就很难看了。

core data里面的讲到类似情况的优化时说,需要把大数据文件新建一个Entity独立出来,然后和以前的Entity建立一对一关系。然而"Core Data Model Versioning and Data Migration Programming"这篇文档完全忽略了怎么样拆分Entity这块内容,今天搜到了一篇文章才知道怎么做。

以下就是一个示范,基于Xcode生成的core data模板程序,并且我假定你已经知道最基本的Model versioning和mapping object的知识。

原始的Model是这样的:

假设我们现在的目标是把address分离到一个独立的entity中去。

现在新建立一个Model Version,假设命名为version2,把它搞成这样:

这样,就把address分离出去了。

接下来要做Migration了,新建一个Mapping Model文件:

根据向导,将source设置为原始的Model文件,target设置为新的Model文件。然后Xcode会生成如下的Mapping文件:

在这个文件基础上,我们只要稍作修改就能完成了。

首先把ENTITY MAPPING下面的Address重命名为EventToAddress(这步可做可不做),然后在右边栏中的Entity Mapping中,设置Source为Event:

接下来改address的Value Expression为$source.address 如下:

然后在左边的ENTITY MAPPINGS栏中切换到EventToEvent,选中Relation Mappings中的address,在右边栏中设置Key Path为$source,设置Mapping Name为EventToAddress(如果刚才没改名,就用刚才的名字)。

然后设置model的current version为version2:

最后一步,在程序中初始化NSPersistentStoreCoordinator的地方,加上NSMigratePersistentStoresAutomaticallyOption的选项,代码类似如下:

 

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 

最后,运行程序,在程序初始化的时候,就会自动做migration了。如果要migration的数据量非常大,就会非常的慢。

完整源代码见:https://gitcafe.com/hikui/iOS-Example/tree/master/CoreDataMigrationDemo

創用 CC 授權條款
本著作係採用創用 CC 姓名標示 2.0 通用版 授權條款授權.
seo service london 说:
2024年1月16日 14:56

On this subject internet page, you'll see my best information, be sure to look over this level of detail

토토사이트추천 说:
2024年1月30日 14:03

Are you looking for the hottest call girls in Islamabad? Look no further! Islamabad is the capital of Pakistan and is home to some of the most stunning call girls in the country. Whether you're looking for a night of fun and pleasure or an unforgettable experience with a beautiful woman, Islamabad call girls offer a variety of services to suit your needs. In this blog post, we'll explore the best places to find the hottest

놈놈놈도메인 说:
2024年1月30日 14:31

This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!.

안전놀이터순위 说:
2024年1月30日 14:52

I have read some excellent stuff here. Certainly price bookmarking for revisiting. I surprise how a lot attempt you place to create this type of great informative web site. 

엠카지노 최신도메인 说:
2024年1月30日 14:53

Positive site, where did you get the information for this post? I'm glad I found it. I'll be checking back soon to see what additional posts you include.

먹튀사이트조회 说:
2024年1月30日 15:04

Positive site, where did you get the information for this post? I'm glad I found it. I'll be checking back soon to see what additional posts you include.

토토핫주소 说:
2024年1月30日 15:11

This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!.

안전놀이터 说:
2024年1月30日 15:20

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. 

토토사이트순위 说:
2024年1月30日 15:23

first-rate facts, precious and amazing layout, as percentage suitable stuff with suitable thoughts and thoughts, plenty of incredible statistics and inspiration, each of which i want, way to provide this type of useful facts here. I used to be genuinely surfing through the net looking for a few information and got here throughout your weblog. I’m impressed through the records which you have in this blog. It suggests how well you recognize this problem. Bookmarked this web web page, will come lower back for extra. Truly exceptional and exciting put up. I used to be searching out this kind of statistics and loved reading this one. Hold posting. Thank you for sharing.

메이저토토주소 说:
2024年1月30日 15:37

If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content. 

토토사이트 说:
2024年1月30日 15:42

Interesting topic for a blog. I was searching the internet for fun and found your website. Great post. Thanks for sharing your knowledge! It's great to see that some people still put effort into maintaining their website. I'll check back soon. 

먹튀검증사이트 说:
2024年1月30日 15:49

I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site

승인전화없는 가입머니 说:
2024年1月30日 16:01

Your article has answered the question I was wondering about!   I would like to write a thesis on this subject, but I would like you to give your opinion once :D

엔트리파워볼 说:
2024年1月30日 16:10

Your article has answered the question I was wondering about!   I would like to write a thesis on this subject, but I would like you to give your opinion once :D

메이저놀이터 说:
2024年1月30日 16:16

I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site

메이저놀이터 说:
2024年1月30日 16:25

Howdy! This post could not be written much better! Reading through this post reminds me of my previous roommate! He always kept talking about this.

텍사스홀덤특징 说:
2024年1月30日 16:45

I found this post while searching for related information in blog search. Great post. Please keep posting and keep your information updated.

라이브카지노역사 说:
2024年1月30日 16:53

hello, i do assume your internet web site need to likely be having browser compatibility problems. On every occasion i test your internet web site in safari, it appears nice but, at the same time as starting off in net explorer, it’s had been given a few overlapping problems. I without a doubt wanted to provide you a brief heads up! Aside from that, notable weblog! You’re so interesting! I do not suppose i’ve definitely read something like that in advance than. So first rate to discover another individual with a few particular mind on this subject matter. Seriously.. Thank you for beginning this up. This internet internet website is one detail this is needed on the net, a person with a bit of originality!

안전공원주소 说:
2024年1月30日 17:02

hello, i do assume your internet web site need to likely be having browser compatibility problems. On every occasion i test your internet web site in safari, it appears nice but, at the same time as starting off in net explorer, it’s had been given a few overlapping problems. I without a doubt wanted to provide you a brief heads up! Aside from that, notable weblog! You’re so interesting! I do not suppose i’ve definitely read something like that in advance than. So first rate to discover another individual with a few particular mind on this subject matter. Seriously.. Thank you for beginning this up. This internet internet website is one detail this is needed on the net, a person with a bit of originality!

สล็อตออนไลน์ 说:
2024年1月30日 17:07

I found this post while searching for related information in blog search. Great post. Please keep posting and keep your information updated.

먹튀검증업체 说:
2024年1月30日 17:14

Very complete and informative explanation. Thank you for this great post. I love your work.

엔트리 说:
2024年1月30日 18:04

This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!.

동행복권PC버전 说:
2024年1月30日 18:18

Positive site, where did you get the information for this post? I'm glad I found it. I'll be checking back soon to see what additional posts you include.

메이저사이트추천 说:
2024年1月30日 18:26

Positive site, where did you get the information for this post? I'm glad I found it. I'll be checking back soon to see what additional posts you include.

안전놀이터 说:
2024年1月30日 18:31

This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!.

먹튀검증사이트 说:
2024年1月30日 18:41

first-rate facts, precious and amazing layout, as percentage suitable stuff with suitable thoughts and thoughts, plenty of incredible statistics and inspiration, each of which i want, way to provide this type of useful facts here. I used to be genuinely surfing through the net looking for a few information and got here throughout your weblog. I’m impressed through the records which you have in this blog. It suggests how well you recognize this problem. Bookmarked this web web page, will come lower back for extra. Truly exceptional and exciting put up. I used to be searching out this kind of statistics and loved reading this one. Hold posting. Thank you for sharing.

먹튀스타 说:
2024年1月30日 18:59

I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site

먹튀큐어 说:
2024年1月30日 19:11

Your article has answered the question I was wondering about!   I would like to write a thesis on this subject, but I would like you to give your opinion once :D

우리카지노 说:
2024年1月30日 19:23

Your article has answered the question I was wondering about!   I would like to write a thesis on this subject, but I would like you to give your opinion once :D

먹튀 说:
2024年1月30日 19:27

I have browsed maximum of your posts. This put up is probably wherein i have been given the maximum. That is a super website, thank you for sharing. There may be no doubt i'd sincerely price it after i read what the idea about this article is. You probable did a nice . You have got done a notable manner with you net web site

메이저놀이터순위 说:
2024年1月30日 19:35

Howdy! This post could not be written much better! Reading through this post reminds me of my previous roommate! He always kept talking about this.

토토커뮤니티모음 说:
2024年1月30日 19:47

Interesting topic for a blog. I was searching the internet for fun and found your website. Great post. Thanks for sharing your knowledge! It's great to see that some people still put effort into maintaining their website. I'll check back soon. 

메이저사이트검증 说:
2024年1月30日 20:01

It is a fantastic post – immense clear and easy to understand. I am also holding out for the sharks too that made me laugh.

사설토토 说:
2024年1月30日 20:19

It is a fantastic post – immense clear and easy to understand. I am also holding out for the sharks too that made me laugh.

실시간배팅사이트 说:
2024年1月30日 20:28

Interesting topic for a blog. I was searching the internet for fun and found your website. Great post. Thanks for sharing your knowledge! It's great to see that some people still put effort into maintaining their website. I'll check back soon. 

먹튀검증커뮤니티 说:
2024年1月30日 20:37

You have done a great job on this article. Very accurate and qualitative. We made it easy to read and understand. You have a real talent for writing. Thank you so much Safe Playground


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter