核心数据不会保存更改

时间:2014-06-18 19:53:52

标签: ios objective-c core-data restkit

我的代码有点问题,我实际上使用RestKit来映射核心数据中的各种对象,这很有效,然后我需要编辑某些对象,当我编辑它时并保存上下文所有工作都很好,没有问题,麻烦是当我停止应用程序并再次打开它时,我所做的任何更改都没有保存,这是我的代码:

[managedObjectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TaskModel.sqlite"];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error %@", error);

//create the managed object contexts
[managedObjectStore createManagedObjectContexts];

//Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

然后这是我的更新方法:

// Get data
    // 1.- Create the request object:
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSManagedObjectContext *localContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
    // 3.- Define the type of managed object you need:
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SavedTask" inManagedObjectContext:localContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    NSPredicate *searchTasktoComplete = [NSPredicate predicateWithFormat:@"taskId == %@", self.task.taskId];
    [fetchRequest setPredicate:searchTasktoComplete];
    // 5.- Execute the request:
    NSError *error;
    NSArray *fetchedTasks = [localContext executeFetchRequest:fetchRequest error:&error];

    SavedTask *taskToComplete = [fetchedTasks objectAtIndex:0];

    //complete the task in core data
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy"];
    NSString *date = [dateFormat stringFromDate:[NSDate date]];
    [taskToComplete setRealDate:date];
    [taskToComplete setFollowUp:@"100"];
    [localContext refreshObject:taskToComplete mergeChanges:YES];
    [localContext save:&error];
    if (![localContext save:&error]) {
        NSLog(@"error");
    }

任何人都可以帮助我吗?,提前谢谢

2 个答案:

答案 0 :(得分:1)

您应该使用saveToPersistentStore方法

NSError *error = nil;
if(![localContext saveToPersistentStore:&error]){
    NSLog(@"Failed to save to data store");
}

答案 1 :(得分:1)

试试这个!

NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } 
}