2014-02-26 76 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並至少記錄錯誤。你可能會拋出一個錯誤,永遠不知道它。