gpt4 book ai didi

iphone - NSFetchedResultsControllerDelegate 未触发

转载 作者:行者123 更新时间:2023-12-03 19:43:09 25 4
gpt4 key购买 nike

我无法弄清楚为什么当我向底层数据存储添加数据时,NSFetchedResultsControllerDelegate 方法没有触发。如果我重新启动 iPhone 应用程序,数据会立即显示。

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

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

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

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

我实现委托(delegate)方法:

- (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];
}

除了我的委托(delegate)方法不触发之外,一切都工作得很好。显然我的表格 View 没有更新,查看新数据的唯一方法是显式刷新或重新启动应用程序。

我浏览了 CoreData Recipe 应用程序 - 但似乎找不到我缺少的内容。想法?

-路德

最佳答案

如果我在创建原始 fetchedResultsController_ 时将 sectionNameKeyPath 从 @"Client"更改为 nil,则 Project 实体创建保存确实调用了委托(delegate)方法!

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

经过严格审查,这就是 CoreData Recipes 示例的作用。随着我的数据变得越来越复杂 - 我认为我将需要该参数来帮助将结果分成几个部分,但现在,很高兴看到委托(delegate)处理程序被调用。

关于iphone - NSFetchedResultsControllerDelegate 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1319940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com