CoreData永久保存?

时间:2010-11-30 21:59:02

标签: iphone objective-c core-data load save

我一直在使用iPad应用中的Core Data,我可以在应用内成功保存和获取数据。但是,当完全关闭应用程序时,完全退出,退出多任务处理,数据就会消失。

在关闭应用程序时,核心数据是否仍保留在任何地方?或者我是否需要寻找其他地方?

编辑:这是在应用代理didFinishLaunchingWithOptions[[[UIApplication sharedApplication] delegate] managedObjectContext];,然后我在UIView子类中有这个:context_ = [(prototypeAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

这是在app delegate中预先制作的NSPersistentStoreCoordinator代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"prototype.sqlite"];

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
         [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

到目前为止,我正在使用它来获取数据:

    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *testEntity = [NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:context_];
    [fetch setEntity:testEntity];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateSaved == %@", datePicker.date];
    [fetch setPredicate:pred];

    NSError *fetchError = nil;
    NSArray *fetchedObjs = [context_ executeFetchRequest:fetch error:&fetchError];
    if (fetchError != nil) {
        NSLog(@"fetchError = %@, details = %@",fetchError,fetchError.userInfo);
    }
    noteTextView.text = [[fetchedObjs objectAtIndex:0] valueForKey:@"savedText"];

这是为了保存数据:

NSManagedObject *newDatedText;
    newDatedText = [NSEntityDescription insertNewObjectForEntityForName:@"DatedText" inManagedObjectContext:context_];
    [newDatedText setValue:noteTextView.text forKey:@"savedText"];
    [newDatedText setValue:datePicker.date forKey:@"dateSaved"];

    NSError *saveError = nil;
    [context_ save:&saveError];
    if (saveError != nil) {
        NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
    }

4 个答案:

答案 0 :(得分:2)

您是否将背景保存在正确的位置?仅在willTerminate中输入后台应用程序状态时,不保存上下文是一个常见错误。

将上下文保存在以下appdelegate方法中:

-(void)applicationDidEnterBackground:(UIApplication *)application

您在插入对象后直接保存上下文,这应该足够了。如果保存后包含任何数据,请检查模拟器中的sqlite文件。

如果

noteTextView.text = [[fetchedObjs objectAtIndex:0] valueForKey:@"savedText"];

不会抛出异常,在上下文中找到一个对象。也许它不包含预期值? 将返回的对象从fetchrequest记录到控制台,看看是否可能出现这种情况

答案 1 :(得分:0)

您应该发布设置NSPersistentStoreCoordinator的代码并添加您的NSPersistentStore。你有没有机会使用NSInMemoryStoreType作为商店的类型?因为这会导致你所看到的行为。或者,您可以每次使用不同的商店路径,这样每次都会给您一个新店。通常,您的商店应该位于“文档”文件夹中,并且每次启动时都应该使用相同的名称。它还应该使用NSSQLiteStoreType

答案 2 :(得分:0)

我发现了这个问题。事实证明,由于它使用了UIDatePicker,在程序开始时它使用以下日期将日期选择器设置为:

NSDate *now = [[NSDate alloc] init];
[datePicker setDate:now];

所以不使用它就可以完美地运行。所以目前我正在寻找这个问题的解决方案,因为这条线似乎会导致问题。

UIDatePicker Interfering with CoreData

答案 3 :(得分:0)

如果在创建项目后添加CoreData。存在冒险在lazy_init NSManagedObject中出错的风险。

-(NSManagedObjectContext*) managedObjectContext{
if (!_managedObjectContext) {
    _managedObjectContext =[self createManageObjectContextWithName:@"name.sqlite"];
}
return _managedObjectContext;}

这是正确的方法:

- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
    return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;}
相关问题