提供委托时,NSFetchedResultsController不会获取

时间:2012-05-12 00:20:17

标签: ios core-data nsfetchedresultscontroller

  • 编辑*所以我似乎将它用于错误的目的,并且永远不会在初始提取时调用委托。我想用它来批量处理提取,因为即使实际提取数据很快,后处理也不是。我只需要卸载到后台进程而不是* Edit *

我在UIViewController的ViewDidLoad方法中创建一个NSFetchedResultsController并将委托设置为self:

self.resultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:[AppDelegate singleton].managedObjectContext sectionNameKeyPath:nil cacheName:nil] autorelease];
self.resultsController.delegate = self;

然后,当点击ViewController时,我会根据需要执行提取:

int indexPathCount = self.indexPaths.count;
int objectCount = self.resultsController.fetchedObjects.count;
if (indexPathCount && objectCount)
{
     [self.tableView beginUpdates];
     [self.tableView insertRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationFade];
     [self.tableView endUpdates];
}
else
{
     NSError* error;
     BOOL success = [self.resultsController performFetch:&error];
     if (!success)
     {
          UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"ERROR" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
          [alert show];
     }
}

上面的代码不会获取任何对象,也不会调用委托方法。如果我注释掉上面的代码行,我将UIViewController指定为委托,那么在第二次浏览被调用的代码时,objectCount将包含正确的值。

  • 编辑*上面的代码现在实际上将获取对象。在第二次运行代码时,objectCount现在正如预期的那样,但仍然没有调用委托方法。现在我的假设是我在内存管理上做了一些不好的事情,但我所有的保留计数似乎都是正确的。 *编辑*

以下是委托方法的实现,但我已经检查了它们的正确性十几次:

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
     [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
     switch(type)
     {
          case NSFetchedResultsChangeInsert:
          {
               NSIndexPath* tableIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:self.section];
               [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:tableIndexPath] withRowAnimation:UITableViewRowAnimationFade];
               [self.indexPaths addObject:tableIndexPath];
          } break;
     }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
     [self.tableView endUpdates];
}

有关为什么NSFetchedResultsController在提供委托时无效的任何想法?

1 个答案:

答案 0 :(得分:0)

信息不足。好吧,不是正确的信息。由于你没有发帖,我将不得不假设...

首先,您的获取请求是什么样的?它是否正确指定要获取的对象?它是否有排序描述符(FRC获取请求的要求)?

通常情况下,当视图控制器加载时你启动请求...手动调用插入数据就像你的代码显示一样,好吧,让我们说非常规,

相关问题