6

I am working on an ipad app where I am dealing with core data.

The data managed by the app can be categorised into two categories.

  • The first kind of data is specific to that device only or app only.
  • whereas the other category of data needs synching among various device having the same app.

so in the scenario, I got a thought to have two model file in my project and two corresponding sqlite files. And synching one sqlite file to order to achieve synching.

Please suggest, if my approach is right and feasible. If not, then please suggest other solutions.

Please try to understand the question. Here I am talking about of two sqlite files having different structure from each other. Means ".xcdatamodel" model files

Cœur
  • 37,241
  • 25
  • 195
  • 267
Harshit Gupta
  • 1,200
  • 12
  • 27
  • 4
    Obviously yes!! You can. – Satheesh Apr 22 '13 at 11:42
  • 2
    But that question was about multiple store files using **the same model**, so it is not really a duplicate. – Martin R Apr 22 '13 at 11:55
  • thanks @MartinR in my case the requirement is two have multiple ".xcdatamodel file.. " – Harshit Gupta Apr 22 '13 at 11:57
  • please suggest solution – Harshit Gupta Apr 22 '13 at 11:57
  • please upvote to carry on discussion. – Harshit Gupta Apr 22 '13 at 12:05
  • @harshitgupta: I have already voted to reopen the question, but why did *you* vote to close it as a duplicate?? - Perhaps you should also try to improve the question. As you can just define 2 models in Xcode and open them separately in your app, what exactly is your problem? – Martin R Apr 22 '13 at 12:18
  • the problem was how to access them once added. Thanks @MartinR a lot – Harshit Gupta Apr 22 '13 at 12:34
  • I agree with @harshitgupta that the duplicate closing was unjust and is not helping anyone. At present, the most efficient way to deal with one or more data models is NSPersistentContainer. I've written an answer showing how to do work with NSPersistentContainer in [Is it possible to have multiple core data “databases” on one iOS app?](https://stackoverflow.com/questions/13109257/is-it-possible-to-have-multiple-core-data-databases-on-one-ios-app/49816946#49816946). I use a 'main' container in appDelegate, and instantiate additional NSPersistentContainers, one per data model, where I need them, – Elise van Looij Apr 13 '18 at 12:36

2 Answers2

18

Possible duplicate here.

You can have any number of data models, provided you create different managed object contexts for each and manage them properly.

- (NSURL *)applicationDocumentsDirectoryForCoreData
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

//first data model

NSURL *modelURL1 = [[NSBundle mainBundle] URLForResource:@"1_model" withExtension:@"momd"];
NSURL *storeURL1 = [[self applicationDocumentsDirectoryForCoreData] URLByAppendingPathComponent:@"1_model.sqlite"];
NSError *error = nil;
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL1];
persistentStoreCoordinator1 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];

if (![persistentStoreCoordinator1 addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL1 options:nil error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

//second model.

 NSURL *modelURL2 = [[NSBundle mainBundle] URLForResource:@"2_model" withExtension:@"momd"];
 NSURL *storeURL2 = [[self applicationDocumentsDirectoryForCoreData] URLByAppendingPathComponent:@"2_model.sqlite"];
 managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL2];
 NSError *error = nil;
 persistentStoreCoordinator2 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];

 if (![persistentStoreCoordinator2 addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL2 options:nil error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

And while taking out the MOC for the store you want:

//select your store - do that in selectStore or a function like that.
NSPersistentStoreCoordinator *coordinator = [self selectStore];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator:coordinator];
    }

Selection between two stores.

-(NSPersistentStoreCoordinator *)selectStore
 {
    if(someCondtion? return persistentStoreCoordinator1: persistentStoreCoordinator2;
 }
Community
  • 1
  • 1
Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • 1
    i am talking about multiple .xcdatamodel file.. is it possible. if yes then how to add that? – Harshit Gupta Apr 22 '13 at 11:53
  • Yes it is possible, .xcdatamodel file means a core data sqlite db. So you can have multiple .xcdatamodel files. – Satheesh Apr 22 '13 at 12:02
  • did you get my point? please upvote to carry on discussion atleast. – Harshit Gupta Apr 22 '13 at 12:03
  • dude I was the first one to upvote your question. Just do it normally man, you would need two NSManagedObject Model NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"one" withExtension:@"momd"]; managedObjectModel1 = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; and another with NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"two" withExtension:@"momd"]; managedObjectModel2 = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; – Satheesh Apr 22 '13 at 12:11
  • thanks buddy... i couldnot find any option to add momd file. til now i have only dealt with xcdatamodel files.. how to add momd files – Harshit Gupta Apr 22 '13 at 12:18
  • momd file is the xcodedatamodel file dude, just try the code I have edited. Add the two xcodedatamodel files and add my code. I guess you will get two .sqlite files in your documents folder. – Satheesh Apr 22 '13 at 12:21
  • See this if you want more info about .mom and .momd files http://stackoverflow.com/questions/10579767/why-the-extension-is-momd-but-not-xcdatamodel-when-search-the-path-for-the-m – Satheesh Apr 22 '13 at 12:27
  • That may not be the best way to do it, but you build upon it. – Satheesh Apr 22 '13 at 12:34
-3

You should use a plist and store that as a file for all device specific things as I suspect it isn't updated that often. Then you can read it into a dictionary or array in one line of code.

So to answer your question, yes you can have as many coredata files as you want, but it can become a hassle to maintain

Trausti Thor
  • 3,722
  • 31
  • 41
  • i am talking about multiple .xcdatamodel file.. is it possible. if yes then how to add that? – Harshit Gupta Apr 22 '13 at 11:55
  • did you get my point? please upvote to carry on discussion atleast. – Harshit Gupta Apr 22 '13 at 12:05
  • You need to make two core data objects, that is remove all the core data stuff from the app delegate, create your own classes around it and initiate them both in f.ex. the app delegate. They can be quite similar, only the filename that needs to change where you set the store object. To add a new core data mapping model, just go to File -> New and pick it there from the Core Data group. But I can't state this strongly enough that you really should not do this, but then again you can add as many core datas as you want, which comes in handy when doing just in memory store. – Trausti Thor Apr 22 '13 at 16:10