我的代码有点问题,实际上我在使用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");
}
有人可以帮我吗?,在此先感谢
您应该使用saveToPersistentStore方法
NSError *error = nil;
if(![localContext saveToPersistentStore:&error]){
NSLog(@"Failed to save to data store");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句