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
406 views
in Technique[技术] by (71.8m points)

objective c - Can multiple (two) persistent stores be used with one object model, while maintaining relations from one to the other?

Introduction

My iOS project ships with a Core Data persistent store weighing some 160MB in SQLite format. There is a ton of grouped information in there, in which users should be able to mark favorites. For this, I need (at least part of) the database to have write capabilities. But of course persistent stores that ship in the application bundle are by design read-only.

If you want the store to have read-write capabilities, you should copy it to, e.g. the app's documents folder. I don't want to do this, because then the app would be twice the size, while the main part of that database is read-only anyway. That would be a waste of resources.

Multiple persistent stores for NSPersistentStoreCoordinator

This is why I thought of using two persistent stores. The first would be the big one in the bundle, and the second could be a small one in the documents folder, storing special "favorite" entities with relationships to the big store.

I know something is possible in this regard, but I can't find the specifics. Should one only use multiple stores if you also have multiple object models? Can one object model be 'distributed' over two persistent stores? When browsing through the Core Data Programming docs, I can't find any real reference about how to set this up. Also Marcus Zarra's book doesn't seem to delve into this topic:

It is possible to add more than one NSPersistentStore to the NSPersistentStoreCoordinator, which can be useful when dealing with data that is split into multiple files. However, in our exam- ple, we have a single file. (Marcus Zarra: "Core Data - Apple's API for Persisting Data on Mac OS X" page 71)

The Question

Who could tell me if what I'm thinking of is possible with Core Data and multiple persistent stores? And could you maybe provide a hint about how to achieve this? Online/offline resources that deal with the topic are very much appreciated too.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The answer is yes. @Caleb points to the right resources, but getting it to work is still quite awkward. I thought I'd place a resumé here:

For two NSPersistentStore instances to share the same model, you have to add a configuration to your model, which is a string-named subset of the entities:

Model configurations

In the model, to an entity that belongs to the second store, you add a fetched property (NSFetchedPropertyDescription for googlability). This is somewhat of a very simple stored procedure, and it could look like this:

NSPredicate format for the fetched property

Then, when you add the stores to your persistent store coordinator, you use the strings for the configuration argument (more info about the options here):

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                          configuration:@"ModifyInBackground" 
                                                    URL:storeURL1
                                                options:options
                                                  error:&error]

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                          configuration:@"ModifyInMain"
                                                    URL:storeURL2 
                                                options:options 
                                                  error:&error]

Finally, when you want to get from the entity in store B to the entity in store A, you trigger the fetched property like you would trigger a fault, just by accessing it.

Note: A fetched property always returns an NSArray, because the predicate you write to establish the link might have multiple results. If you want to get to just one entity, you could place something like this in a wrapper method of your NSManagedObject subclass:

Wallpaper *recordedWallpaper = [record.wallpaper lastObject];

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

...