NSFetchedResultsControllerDelegate不触发 [英] NSFetchedResultsControllerDelegate not firing

查看:135
本文介绍了NSFetchedResultsControllerDelegate不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,在我的生活,NSFetchedResultsControllerDelegate方法不触发时,我添加数据到基础数据存储。如果我重新启动iPhone应用程序,数据立即显示。

I can't figure out why, for the life of me, the NSFetchedResultsControllerDelegate methods are not firing when I add data to the underlying datastore. The data shows up immediately if I restart the iPhone application.

我已经子类化UITableViewController并符合NSFetchedResultsControllerDelegate:

I have subclassed UITableViewController and conform to NSFetchedResultsControllerDelegate:

@interface ProjectListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
    NSFetchedResultsController* fetchedResultsController_;
    NSManagedObjectContext* managedObjectContext_;
}

我实例化NSFetchedResultsController并将委托设置为self:

I instantiate the NSFetchedResultsController and set the delegate to self:

// Controller
fetchedResultsController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:request
    managedObjectContext:self.managedObjectContext 
                                                                  sectionNameKeyPath:@"Client" 
                                                                           cacheName:@"ProjectsCache"];
fetchedResultsController_.delegate = self;

我实现委托方法:

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
    NSLog(@"ProjectListViewController.controllerWillChangeContent");
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller { ... }
- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { ... }
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { ... }

希望保存:

Project* newProject = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
ProjectDetailViewController* detail = [[ProjectDetailViewController alloc] initWithStyle:UITableViewStyleGrouped 
                                                                                delegate:self 
                                                                                selector:@selector(finishedAdding:) 
                                                                                 project:newProject];

之后,我保存:

- (void)save {
    // NSLog(@"ProjectDetailViewController.save");
    self.project.name = projectNameTextField_.text; 
    NSError* error;
    BOOL b = [self.project.managedObjectContext save:&error];
    if (!b) {
        NSLog(@"Error saving project!");
    } else {
        NSLog(@"Project was successfully saved.");
        [delegate_ performSelector:selector_ withObject:self.project];
    }
    [self dismissModalViewControllerAnimated:YES];
}

这一切都很好,除了我的委托方法不火。显然,我的表视图没有更新,唯一的方式来看到新的数据是显式刷新或重新启动应用程序。

It all works just fine except for the fact that my delegate methods don't fire. Obviously my table view doesn't get updated and the only way to see the new data is to explicitly refresh or restart the app.

我浏览过CoreData Recipe应用程序 - 但似乎找不到我错过了。

I've looked through the CoreData Recipe app - but can't seem to find what I'm missing. Thoughts?

-Luther

推荐答案

当我创建原始的 fetchedResultsController _ 时, Project c $ c> c $ c> sectionNameKeyPath code>实体 保存确实调用了委托方法!

If I change the sectionNameKeyPath from @"Client" to nil when I create the original fetchedResultsController_, both the Project entity creation and save indeed invoke the delegate methods!

fetchedResultsController_ = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                        managedObjectContext:self.managedObjectContext 
                                          sectionNameKeyPath:nil
                                                   cacheName:@"ProjectsCache"];

经过仔细审查,这就是CoreData Recipes例子。因为我的数据变得更复杂 - 我想我需要这个参数帮助将结果分成几个部分,但现在,很高兴看到委托处理程序被调用。

Upon hyper scrutiny, that is what the CoreData Recipes example does. As my data gets more complex - I think I'm going to need that argument to help break the results up into sections but for now, it is nice to see the delegate handlers being invoked.

这篇关于NSFetchedResultsControllerDelegate不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆