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?
Two persistent stores for one managed object context - possible?
Asked Answered
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.
+1 To expand, you would use configurations and put the entities that need to be stored in disk in one configuration and the in-memory ones in another. Assign the appropriate configurations to the appropriate store when you create the stores. Be aware that you can't use direct relationships across stores but must rely on fetched relationships instead. –
Loving
You may use configurations as TechZen mentioned:
- Create Configurations in managed object model editor (.xcdatamodel file);
- In code add several persistent stores to persistent store coordinator, providing appropriate configuration name.
For details check my other answer here.
© 2022 - 2024 — McMap. All rights reserved.