2014-02-26 74 views
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]; 

但就像我说的,当我重新回到表视图后,立即新的对象不显示创造记录。什么是问题?

+0

您不需要保存上下文两次。记录你正在使用的'error'变量。 – Wain

+0

您是否收到任何错误,是否重新载入表数据? –

+0

使用fetchedResultsController与您的UITableView结合使用 - 请参阅https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html –

回答

1

听起来像你没有正确实施NSFetchedResultsController的委托方法。虽然你没有分享你的任何视图代码,所以无法确定。

此外,保存后,您不检查从-save:返回BOOL。你应该总是,总是总是检查BOOL并至少记录错误。你可能会抛出一个错误,永远不知道它。