Two persistent stores for one managed object conte

2019-02-22 10:12发布

问题:

I have a fairly complex data model with approximately 10 entities. Some need to be stored to disk and others just need to be available in memory when the application is running. Is it possible to achieve this using two persistent stores for the same managed object context, or should I separate my data models accordingly?

回答1:

Yes, your NSManagedObjectContext uses a NSPersistentStoreCoordinator to determine which store a particular model should use. By setting the persistent store coordinator of your managed object context you can define a custom mapping which uses multiple persistent stores of different types.

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650-SW4



回答2:

You may use configurations as TechZen mentioned:

  1. Create Configurations in managed object model editor (.xcdatamodel file);
  2. In code add several persistent stores to persistent store coordinator, providing appropriate configuration name.

For details check my other answer here.