我正在使用NSFetchedResultsController来填充我的UITableViewController的内容。
我正在使用NSOperation从webService收集数据(我使用的是分离的ManagedObjectContext,因为它是另一个线程)
保存数据时,我的ViewController(NS是NSFetchedResultsControllerDelegate)被调用,我使用mergeChangesFromContextDidSaveNotification合并我的MOC
#pragma mark -
#pragma mark Parsers delegate
- (void)parserWillSave:(id)parser{
TopNewsParser *emp = (TopNewsParser *)parser;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(parserContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:emp.managedObjectContext];
[NSFetchedResultsController deleteCacheWithName:@"aLaUne"];
}
- (void)parserDidSave:(id)parser{
TopNewsParser *emp = (TopNewsParser *)parser;
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:emp.managedObjectContext];
}
/**
Notification from the add controller's context's save operation. This is used to update the fetched results controller's managed object context with the new book instead of performing a fetch (which would be a much more computationally expensive operation).
*/
- (void)parserContextDidSave:(NSNotification*)saveNotification {
DLog(@"");
NSManagedObjectContext *fetchContext = [_fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
[fetchContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:saveNotification
waitUntilDone:YES];
}
对于NSFetchedResultsControllerDelegate,我正在使用CoreData Books示例中的代码
#pragma mark -
#pragma mark NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
//ALog(@"indexPath: %@ newIndexPath : %@ | type : %d # %@",indexPath,newIndexPath,type,anObject);
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];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (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)controllerDidChangeContent:(NSFetchedResultsController *)controller {
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refreshTableViewContent)];
reloadButton.accessibilityLabel = @"Reload";
self.navigationItem.leftBarButtonItem = reloadButton;
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];
}
我的问题是当加载新内容时某些对象被删除,我的tableView搞砸了!即使我的countOfRow减少1,该行仍然可见:
然后当我向下滚动我的tableview是空的(只有4个可见的行)仍然在tableview中,否则它是一个空白的scrollView
在控制台中,我可以看到以下消息。
严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常。 *** - [NSMutableArray removeObjectAtIndex:]:索引0超出userInfo的空数组的边界(null)
在开始尝试时,这是由于我的NSFetchedResultsController的缓存,但即使我禁用它,我也有同样的问题。
有人知道如何解决这个问题吗?
答案 0 :(得分:0)
这里也有同样的问题。
人们建议使用TopSongs示例代码。修改版本在这里: http://www.bigbluebrains.com/index.php/2010/08/16/iphone-topsongs-sample-code-memory-leak-fix/
我想知道是否应该在共享数据上使用互斥锁。