Removing MagicalRecord from app, can't see data via Core Data methods

I have an app that currently uses MagicalRecord to store its data. As of 2017, MR is toast, so I've decided to remove it and go with standard Core Data (since it's been updated and is easier to use now).

My problem is that I can't seem to get the new Core Data methods to see the data in the store. Here's how it's set up:

Old version (MagicalRecord):
Code Block Objective-C
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"MyModel"];


New version (Core Data):
Code Block Objective-C
self.persistentContainer = [[NSPersistentContainer alloc] initWithName:@"MyModel"];
[self.persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *description, NSError *error) {
if(error != nil) {
NSLog(@"Failed to load Core Data stack: %@", error);
abort();
} else {
self.viewContext = self.persistentContainer.viewContext;
}
}];


If I load the old version into Xcode 12b6 and run it on the iOS 13.7 Simulator I can read, update and delete data in the store, and it all works properly as before.

When I load the new code into Xcode 12b6 and run it in the same iOS 13.7 Simulator, the app launches but there is no data in the store.

Any data in the old version is not seen in the new version, and vice versa. It's like they're two different stores of data.

The new version can see the store because I change the name to, say, "MyModel99" it errors saying it can't find MyModel99.

I can re-launch the old code into the Simulator and the data is there, so I think the data model is all fine, it's just that I can't seem to get the new code to read that data.

Any ideas? I'm guessing there's something I'm missing when setting up the persistent container etc.? I guess the question is: how do you migrate data from MR to Core Data? If MR is basically a wrapper for CD, why can't I see the data in it?

Thanks.
Answered by darkpaw in 631380022
Well, that wasn't easy.

The fix - for anyone out there who needs to do this - is to find out where MagicalRecord has saved your data store, and either tell Core Data that location, or to move the MR store file(s) to the location Core Data wants to use by default.

In my case, MR had stored them inside <my app's folder>/Library/Application Support/<my app's name>/

Core Data was looking in <my app's folder>/Documents/

Simple fix, but gods this took ages to figure out. Would've been lovely for the MagicalRecord developers to have provided some easy instructions on how to migrate away from MR after they shut it down ¯_(ツ)_/¯
Updated code.

In AppDelegate.m I do this:
Code Block Objective-C
self.dataController = [[DataController alloc] init];
self.viewContext = self.dataController.managedObjectContext; // This is an NSManagedObjectContext property


This is the init method of my DataController class:
Code Block Objective-C
- (id)init
{
self = [super init];
if(!self) return nil;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyModel" withExtension:@"momd"];
NSAssert(modelURL, @"Failed to locate momd bundle in application");
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSAssert(managedObjectModel, @"Failed to initialise Managed Object Model from url: %@", modelURL);
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
[self setManagedObjectContext:managedObjectContext];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSPersistentStoreCoordinator *psc = [[self managedObjectContext] persistentStoreCoordinator];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [documentsURL URLByAppendingPathComponent:@"MyModel.sqlite"];
NSDictionary *options = @{NSInferMappingModelAutomaticallyOption : @(true), NSMigratePersistentStoresAutomaticallyOption : @(true)};
NSError *error = nil;
NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];
if(!store) {
NSLog(@"Error initialising Persistent Store: %@\n%@", [error localizedDescription], [error userInfo]);
abort();
}
});
return self;
}


All this code runs without error. A context is created and I can read and write to it, BUT I still cannot see the original data stored by the MagicalRecord version. Why can't I see that data?

I cannot find ANY info on the internet about accessing your data store if you move away from MagicalRecord and go with Core Data, which seems like something hundreds of people will have done.

Please help. I can't spend more days on this - seemingly - simple problem.
Accepted Answer
Well, that wasn't easy.

The fix - for anyone out there who needs to do this - is to find out where MagicalRecord has saved your data store, and either tell Core Data that location, or to move the MR store file(s) to the location Core Data wants to use by default.

In my case, MR had stored them inside <my app's folder>/Library/Application Support/<my app's name>/

Core Data was looking in <my app's folder>/Documents/

Simple fix, but gods this took ages to figure out. Would've been lovely for the MagicalRecord developers to have provided some easy instructions on how to migrate away from MR after they shut it down ¯_(ツ)_/¯
Removing MagicalRecord from app, can't see data via Core Data methods
 
 
Q