核心数据不持久:使用 magicalrecord

Core Data not persisting: Using magicalrecord

好的,

我正在使用 Magicalrecord 来实现核心数据。我的应用程序在第一次加载时下载了一堆信息。然后将该数据保存到核心数据。这是在 AppDelegate 文件中完成的。但是,每当我的应用程序进入后台或终止时,数据就会丢失。这是带有选项方法的 didFinishLaunching:

- (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;
}

这是我在 applicationDidEnterBackground 和 ApplicationWillTerminate 中使用的方法:

- (void)saveContext {
[MagicalRecord cleanUp];

}

感谢您对此提供的任何帮助。我已经阅读了这里的每个 magicalrecord 非持久性问题,但没有找到任何有效的方法。

保重,

对不起,

经过进一步探索,我发现设置2个不同名称的核心数据堆栈通常是行不通的,如果您希望它们是同一个堆栈...我已更正此问题。