Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 database, 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?

like image 557
Darshan Prajapati Avatar asked Aug 02 '11 13:08

Darshan Prajapati


People also ask

What is persistent store in Core Data?

A persistent store is a repository in which managed objects may be stored. You can think of a persistent store as a database data file where individual records each hold the last-saved values of a managed object. Core Data offers three native file types for a persistent store: binary, XML, and SQLite.

What is persistent store iOS?

A persistent storage is a repository, which stores managed objects. Database is the most common way to store and manage data.

Where is data stored in Core Data?

The persistent store should be located in the AppData > Library > Application Support directory. In this example you should see a SQLite database with extension . sqlite. It is possible that you don't see the persistent store in the Application Support directory.

How do I store UIColor in Core Data?

In order to save a UIColor to core data you need to separate the red, blue, and green values. Then, within your creation code, fetch the RGB values and create a UIColor object with the fetched results.


2 Answers

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

.xcdatamodel file in XCode

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

like image 157
MANIAK_dobrii Avatar answered Sep 22 '22 12:09

MANIAK_dobrii


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.

like image 44
casademora Avatar answered Sep 21 '22 12:09

casademora