Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

ios - coredata - move to app group target

I am new to the Today extension and using an embedded framework.

Our app currently uses core data backed by sqlite. If I want to share this between the app and the today extension, should I move this to a framework to be shared by both?

How can I migrate our current version in the app store to be able to upgrade to a new structure?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make sure both the model and the persistent store file are available to both the app and the extension.

For the model, moving it to a framework is a good idea since it means there's only one copy of the model file. As long as both the app and the extension link to the framework, it'll be available to both. If you do that, it's probably a good idea to put the code that sets up the Core Data stack in the framework too, since it'll be the same in both cases.

You can of course just include the model in both targets. That will mean you'll have two copies of the file, which wastes space. Probably not a lot of space, though.

For the persistent store, you'll have to set up an app group and use a store file in the group directory. App groups is one of the settings in "capabilities" for the app and extension-- turn it on and create a group name. Then put the persistent store file in the group directory, which you can find using code like

NSURL *groupURL = [[NSFileManager defaultManager]
    containerURLForSecurityApplicationGroupIdentifier:
        @"GROUP NAME HERE"];

[I cover some of this in more detail at my blog].

If you have existing data, you'll have to move it to the new store file. This would look something like

  1. Check if the old non-group copy of the data exists
  2. If it does, set up a Core Data stack using that file. Then use migratePersistentStore:toURL:options:withType:error: to move it to the new location. Then remove the old copy.
  3. If the old version doesn't exist, just set up Core Data with the new copy as usual.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...