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 –