如何在 tableView 行选择时更新我的 fetchedResultsController?
Posted
技术标签:
【中文标题】如何在 tableView 行选择时更新我的 fetchedResultsController?【英文标题】:How can I update my fetchedResultsController upon tableView row selection? 【发布时间】:2011-11-04 00:13:03 【问题描述】:好的,我在 Core Data 方面做得越来越好,但我还有很长的路要走。这就是我在加载视图时填充 fetchedResultsController 的方式:
- (NSFetchedResultsController *)fetchedResultsController
if (__fetchedResultsController != nil)
return __fetchedResultsController;
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Visit" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Queue"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"(isActive == YES)"];
[fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
return __fetchedResultsController;
这很好用。我将 fetchedResultsController 连接到我的 tableView 并且我有 5 个托管对象正在显示。我遇到的问题是当我需要对其中一个托管对象进行更改时。
正如您在谓词中看到的,我指定我只想要具有isActive == YES
的托管对象。我需要将托管对象的 isActive 状态更改为 NO,然后将其从 fetchedResultsController 中删除,最终从 tableView 中删除。
这是我的尝试:
-(void) seatedButton
Visit * vis = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:reloadIndex inSection:0]];
vis.isActive = [NSNumber numberWithBool:NO];
NSError *error = nil;
if (![self.managedObjectContext save:&error])
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
[tableView reloadData];
这似乎可行,但它没有。当 tableView 重新加载时,有 5 个对象符合谓词的要求,而它应该只有 4 个!
我在这里做错了什么?我需要做什么来更新 fetchedResultsController?谢谢!
【问题讨论】:
【参考方案1】:您可能想要使用的 fetchedResultsController 的委托方法应该会自动为您完成所有这些工作。保存上下文后,它会为您更新表格,因此您不必调用 [tableView reloadData]
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
[self.tableView beginUpdates];
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
switch(type)
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
UITableView *tableView = self.tableView;
switch(type)
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
[self.tableView endUpdates];
编辑:如果你这样做,你将需要实现以下方法:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
// configure cell;
【讨论】:
感谢您的回答。事实上,我确实实现了所有这些委托方法(包括 configureCell(感谢 Ray Wenderlich)),但它仍然没有删除单元格。这让我发疯了! 我假设 configureCell 方法以及您的 tableView 数据源方法都基于 fetchedResultsController?我可以想象如果它仍然没有更新,为什么它会让你发疯,因为看起来你做的一切都是正确的。调用[self.managedObjectContext save:&error]
应该会相应地自动更新所有内容。只是我要检查的一些东西:确保 managedObjectContext 不为空,并在委托方法中放置一些日志以查看它们是否被调用。此外,这些都不是在后台线程中进行的,是吗?如果是这样,那可能会失去对上下文的把握【参考方案2】:
我很确定将以下代码添加到您的 seatedButton 方法中,就在 [tableView reloadData] 行之前将解决问题,但根据文档不确定是否有必要。
if (![self.fetchedResultsController performFetch:&error])
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
【讨论】:
以上是关于如何在 tableView 行选择时更新我的 fetchedResultsController?的主要内容,如果未能解决你的问题,请参考以下文章
在选择 tableview 项目时更新 ViewController 文本框
如何在更新 tableview 时使用搜索栏对我的 NSFetchedResults 数据进行排序,而不会在 fetchedRC.performFetch() 上崩溃?更新