Alright,
I'm using Magicalrecord for core data implementation. My app downloads a bunch of info the first time it is loaded. That data is then saved to core data. This is done in the AppDelegate file. But, whenever my app goes to the background or is terminated the data is lost. Here is the didFinishLaunching with options method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"My app is starting");
[MagicalRecord setupAutoMigratingCoreDataStack];
//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"AGM_DataSetup"]) {
NSError* jsonError;
NSArray* json;
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus == NotReachable) {
NSLog(@"Offline");
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"static" ofType:@"json"];
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
json = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
} else {
NSLog(@"Online");
NSData* data = [NSData dataWithContentsOfURL:
aWaterURL];
json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&jsonError];
}
if (!jsonError) {
[MagicalRecord cleanUp];
NSString* folderPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error])
{
[[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
if(error)
{
NSLog(@"Delete error: %@", error.description);
}
}
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"Anglers411.sqlite"];
NSLog(@"Initial Data Load");
for (id jsonArea in json) {
// Create an area
Area *area = [Area MR_createEntity];
area.name = [jsonArea objectForKey:@"name"];
area.bulletDescription = [jsonArea objectForKey:@"bulletDescription"];
area.uid = [jsonArea objectForKey:@"id"];
NSArray* jsonLocations = [jsonArea objectForKey:@"locations"];
for (id jsonLocation in jsonLocations) {
// create a location
Location *location = [Location MR_createEntity];
location.uid = [jsonLocation objectForKey:@"id"];
location.name = [jsonLocation objectForKey:@"name"];
location.desc = [jsonLocation objectForKey:@"desc"];
location.directions = [jsonLocation objectForKey:@"directions"];
location.bulletDescription = [jsonLocation objectForKey:@"bulletDescription"];
location.area = area;
NSArray* jsonBugs = [jsonLocation objectForKey:@"bugs"];
for (id jsonBug in jsonBugs) {
Bug *bug = [Bug MR_createEntity];
bug.name = [jsonBug objectForKey:@"name"];
bug.spring = [jsonBug objectForKey:@"spring"];
bug.summer = [jsonBug objectForKey:@"summer"];
bug.fall = [jsonBug objectForKey:@"fall"];
bug.winter = [jsonBug objectForKey:@"winter"];
bug.location = location;
}
}
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"You successfully saved your context.");
} else if (error) {
NSLog(@"Error saving context: %@", error.description);
}
}];
// Set User Default to prevent another preload of data on startup.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AGM_DataSetup"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
return YES;
}
Here is the method I use in both applicationDidEnterBackground and ApplicationWillTerminate:
- (void)saveContext {
[MagicalRecord cleanUp];
}
Thanks for any help with this. I have read over every magicalrecord non-persist question on here and haven't found anything that worked.
Take Care,
Ben
Sorry,
After further exploration, I see that it is generally not going to work to set up 2 core data stacks with different names, if you want them to be the same stack... I have corrected this.