Which persistent store is used by default in core data in iPhone

前提是你 提交于 2019-11-30 13:55:59

Fetching should not be an issue. The fetch request can be modified to search particular stores using the setAffectedStores: method on an NSFetchRequest.

When you're creating an object, you can assign the entity to a particular store using the assignObject:toPersisentStore: method on NSManagedObjectContext.

As to your question, there isn't really a default mechanism that I am aware of, and it may be that you simply need to set the affected stores to all of your stores:

[request setAffectedStores:[NSArray arrayWithObjects:firstStore,secondStore,thirdStore, nil]];

To be sure that you're looking in all the right places.

You may use configurations.

[PersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:nil error:]

Say you want to have single managed object context, single managed object model, single persistent store coordinator but two persistent stores, for example first one will be SQLite store and second one will be a in-memory store.

For this setup you create two configurations, "SQLiteStore" for SQLite store and "InMemoryStore" for in-memory store. In XCode (open your .xcdatamodel file):

you see list of available configurations of your managed object model. Managed object model configuration is basically a set of entity descriptions associated with a string name. To add configuration use Editor -> Add Configuration main menu item while you have .xcdatamodel file opened, then type a string name you prefer. Drag entities you want to be stored in first SQLite store to "SQLiteStore" configuration and others to "InMemoryStore" configuration.

Ok, that's it, now it's time to update your code. Go to the scope, where you create persistent store coordinator and add persistent stores to it. The only change is specifying configuration for them:

...
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:yourManagedObjectModel];
NSURL storeURL = … // your store url
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"SQLiteStore" URL:storeURL options:nil error:&error])
{
    NSLog(@"[Core Data error] Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

if (![__persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:@"InMemoryStore" URL:nil options:nil error:&error])
{
    NSLog(@"[Core Data error] Unresolved error %@, %@", error, [error userInfo]);
    abort();
} 
...

That's it now, all entities you've dragged to "InMemoryStore" configuration will be automatically saved to in-memory persistent store and the same goes for "SQLiteStore". Maybe you'll have to reinstall your app on the device/simulator after that.

And a fast resume:

  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.

Check this link for more info: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650-SW4

I think you really want to use a PSC for each store. This will make the problems you describe go away and I can't really see why you would want to have just the one PSC.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!