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):
New version (Core Data):
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.
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.
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 ¯_(ツ)_/¯
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 ¯_(ツ)_/¯