0
我只在重新启动我的应用程序后才看到保存的上下文。我有两个视图控制器,一个允许用户保存核心数据记录,另一个显示。当我保存一条新记录时,然后转到表格视图查看所有创建的记录,我没有看到新记录。这是我创造了纪录:核心数据保存上下文
NSManagedObjectContext *context = [self managedObjectContext];
Property *myProperty = [NSEntityDescription
insertNewObjectForEntityForName:@"Property"
inManagedObjectContext:context];
myProperty.aptDescription = curProperty.description;
NSError *error;
[context save:&error];
[appDelegate saveContext];
然后我retreive它这样做:
ATAppDelegate *appDelegate = (ATAppDelegate *)[[UIApplication sharedApplication]delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Property" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.myProperties = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
但就像我说的,当我重新回到表视图后,立即新的对象不显示创造记录。什么是问题?
您不需要保存上下文两次。记录你正在使用的'error'变量。 – Wain
您是否收到任何错误,是否重新载入表数据? –
使用fetchedResultsController与您的UITableView结合使用 - 请参阅https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html –