核心数据不存在:使用magicalrecord

时间:2015-05-14 16:23:57

标签: ios 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];

}

感谢您对此提供任何帮助。我已经在这里阅读了每个魔法记录的非持久性问题,并且没有找到任何有用的东西。

保重,

1 个答案:

答案 0 :(得分:0)

对不起,

经过进一步的探索,我发现如果你想让它们成为同一个堆栈,通常不会设置2个不同名称的核心数据堆栈......我已经纠正了这个问题。