0

I'm working on a small iPhone logging app and I want to keep a database of location data separate from the users logged data.

I have several reasons for separating the two including

1) I'll probably push periodic location data updates with app updates and I don't want to risk making a cockup and affecting the users data.

2) the location data will probably dwarf the user data so synching and backing up the users file will happen quicker if the two are separate.

So given I intend to have two persistent stores are there advantages or disadvantages in terms of performance and coding complexity to having one context with two persistent stores in it over 2 separate contexts each with their own persistent store?

Any thought on this would be greatly appreciated - i'm a bit of a n00b when it comes to this stuff - thanks in advance

Simon .

SimonTheDiver
  • 1,158
  • 1
  • 11
  • 24

2 Answers2

1

I have implemented a similar thing in my app, I load data from 2 databases because I want to push updates to a certain set of data.

The way I have done it is using 1 object model, 1 managed object context but 2 persistent stores, each one loading a different sqlite file. To do this I created 2 configurations in data model file then dragged the appropriate entities into each configuration. Then when you create the persistent stores do it like this:

[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Configuration1" URL:store1URL options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil] error:&error];
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"Configuration2" URL:store2URL options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil] error:&error];

The only problem I have found with this so far is you cant have relationships between stores. This answer shows how to use a fetched property to replicate a relationship.

Hope this helps.

Community
  • 1
  • 1
JDx
  • 2,615
  • 3
  • 22
  • 33
  • Thanks for that and the link to the other answer. I had been thinking of two separate models but I think I see what you are doing with the one model (which presumably is loading certain entities from each of the persistent stores). It doesn't however answer the question of is there a performance advantage of a single context implementation over a two separate contexts. Any thought on that ? – SimonTheDiver Jan 25 '13 at 15:50
0

I actually was able to use two different managedObjectContexts with two different Models and two different persistent Stores using the same application.

It was just a matter of having them load on the Application Delegate and they are available throughout the app.

What I did is that I just copy and pasted the exact same initialization default code for the first context and renamed it for the new context, store and model.

Why did I do this? There was a completely separate module of the app that I was handed and I did not have much control on it and I did not wanted to mess up the logic of my app and be able to keep it in a separate fork on the code repository, doing differentiated commits and cherry picking enhancements for the main branch.

GZepeda
  • 53
  • 5