我有UITableViewController
个任务项,使用Apple的NSFetchedResultsController
(子类)模板通过CoreDataTableViewController
连接到Core Data。 TVC打开detailViewController,处理编辑和保存。
有时当我在Core Data中保存任务并编辑一个任务然后保存它时,detailViewController会保存数据并返回到TVC,但tableView根本没有显示任何记录,它只是空的。当我杀死应用程序并再次运行它时,所有任务都会按预期显示。
这是非常罕见的行为,因此很难跟踪和调试。在极少数情况下,有没有人有什么想法导致空tableView?
在DetailVC.m
- (void)save
{
if (self.editingTask) {
[[DataManager sharedInstance] updateItem:self.editingTask withText:self.textField.text date:self.datePicker.date priority:self.segmentedControl.selectedSegmentIndex completed:NO];
} else {
[[DataManager sharedInstance] saveNewItem:self.textField.text date:self.datePicker.date priority:self.segmentedControl.selectedSegmentIndex];
}
[self.navigationController popViewControllerAnimated:YES];
}
在DataManager.m
- (CoreDataStatus)updateItem:(Task *)editingTask withText:(NSString *)text date:(NSDate *)date priority:(NSUInteger)priority completed:(BOOL)completed
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
Task *taskToUpdate = editingTask;
taskToUpdate.due = date;
taskToUpdate.text = text;
taskToUpdate.priority = [NSNumber numberWithInteger:priority];
taskToUpdate.completed = [NSNumber numberWithBool:completed];
// save
NSError *error = nil;
[appDelegate.managedObjectContext save:&error];
if (!error) {
[self updateNotificationsAndBadgeCount];
return Success;
} else {
return Error;
}
}
在TVC.m
- (void)awakeFromNib
{
self.managedObjectContext = self.appDelegate.managedObjectContext;
}
- (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
_managedObjectContext = managedObjectContext;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Task"];
request.predicate = [NSPredicate predicateWithFormat:@"completed = %@", [NSNumber numberWithBool:NO]];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"due" ascending:YES]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
}
在CoreDataTableViewController.m
中- (void)performFetch
{
if (self.fetchedResultsController) {
if (self.fetchedResultsController.fetchRequest.predicate) {
if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate);
} else {
if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName);
}
NSError *error;
BOOL success = [self.fetchedResultsController performFetch:&error];
if (!success) NSLog(@"[%@ %@] performFetch: failed", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
} else {
if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
[self.tableView reloadData];
}