NSPersistentStoreCoordinator with two types of persistent stores?
In an iOS app, I would like to use an
NSPersistentStoreCoordinator with both an
NSIncrementalStore subclass, for fetching data from a REST API, but also with an SQLite store, to save to disk. If I add both types of persistent stores to my coordinator, however, calling
save: on my managed object context has no effect. If I only add the one persistent store, not of the type for my
NSIcrementalStore subclass, then the save works as intended.
Is there any way to achieve this functionality?
- EXC_BAD_ACCESS error when trying to change Bool property
- swift sync data of core data an mysql database
- Swift CoreData: error: Failed to call designated initializer on NSManagedObject class 'NSManagedObject'
- Why is my transformable Core Data attribute not using my custom NSValueTransformer?
- iOS: How can I create a backup copy of my core data base? And how to export/import that copy?
- NSFetchedResultsController and NSOrderedSet relationships
Solutions Collect From Internet About “NSPersistentStoreCoordinator with two types of persistent stores?”
The best solution in my experience is to have multiple managed object contexts, each having its own model.
However, there is a way to accomplish what you want:
// create the store coordinator NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] init]; // create the first store NSPersistentStore *firstStore = [storeCoordinator addPersistentStoreWithType: NSIncrementalStore configuration:nil URL:urlToFirstStore options:optionsForFirstStore error:&error]; // now create the second one NSPersistentStore *secondStore = [storeCoordinator addPersistentStoreWithType:NSSQLiteStore configuration:nil URL:urlToSecondStore options:optionsForSecondStore error:&error]; // Now you have two stores and one context NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; [context setPersistentStoreCoordinator:storeCoordinator]; // and you can assign your entities to different stores like this NSManagedObject *someObject = [[NSManagedObject alloc] initWithEntity:someEntity insertIntoManagedObjectContext:context]; // here the relevant part [context assignObject:someObject toPersistentStore:firstStore]; // or secondStore ..
You should also check these links to get a better idea about how Core Data works:
Core Data Programming Guide – Persistent Store Coordinator
SO: Two persistent stores for one managed object context – possible?
SO: Can two managed object context share one single persistent store coordinator?
Also check the comment by TechZen in the second link about configurations and read about it in here:
Core Data Programming Guide – Configurations
and here is a nice tutorial to manage two object contexts:
Multiple Managed Object Contexts with Core Data
- Dictionary wrong order – JSON
- Xcode 8.0 Command /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
- Limit app to running only on 4 inch devices IOS
- Implement Google Analytics in ios swift
- Why do I get the error “error: unknown type name 'virtual'” when trying to compile this code?
- How to get Google Map's 'myLocationEnabled' blue dot always on top
- Swift does not conform to protocol
- Sending IOS notifications from Firebase (or another platform) to specific terminales
- Debug iOS 6+7 Mobile Safari using the Chrome DevTools
- iOS Enterprise In-House Deployment need for Device IDs?
- IOS scrollview ambiguous scrollable content height in case of autolayout
- Creating a Customized UITableView
- Xcode: simulators shows correct xib interface, but on device, it's showing old interface
- looping through an NSMutableDictionary
- SpriteKit physics in Swift – Ball slides against wall instead of reflecting