开发者

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

I am creating multiple persistent store in my application, but I am using only one persistent store coordinator and managed object model. Now my question is when I call save method on managed object context, which persistent store it will use to save the object. So I want to specify the persistent store to be used to save the object. Same while fetching the objects from databas开发者_运维技巧e, I want to ensure that my fetch query should be executed on a particular store so that I get objects from that store only. Any help?


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):

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

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜