NSFetchedResultsController -sectionNameForKeypath 如何获取其数据?
我一直在使用 NSFetchedResultsController 做一些噩梦,并试图找到解决我的问题的方法 在这里,我发现 fetchedResultsController 获取的数据有些不寻常,特别是 sectionNameForKeypath
参数。
假设我有一个包含 2 个实体和关系的对象模型,如下所示:
Item <<--> Category
Category 有一个名为 displayOrder
的 (NSNumber*) 属性,用于将获取的 Item 实例分隔成表视图中的部分:
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"category.displayOrder"
cacheName:nil];
如果我想要以某种方式动态地操纵 displayOrder
的输出,显而易见的选择是覆盖它的 getter 方法并在那里做我想做的任何事情。但问题是,即使我在 sectionNameForKeypath
中指定了 displayOrder
的 getter,在 fetchRequest 期间或 fetchedResultsController 也根本不会访问它,而且我可以看到fetchedResultsController 使用它的值来分隔和显示各个部分。
下面是一个被覆盖的 getter 方法的简单示例,该方法永远不会被调用。这被放置在我的 Category 类中:
-(NSNumber*)displayOrder
{
NSLog (@"displayOrder getter called");
NSNumber *displayOrder = [NSNumber numberWithInt:0];
return displayOrder;
}
我也尝试覆盖 awakeFromFetch
无济于事(也没有被调用,但这并不令我感到惊讶,因为我正在获取 Item 实例并访问类别及其属性(通过关系)。
所以最后我的问题是:category.displayOrder 值如何传递到我的 fetchedResultsController 以及有没有办法让我控制这个输出?
预先感谢您的任何想法!
罗格
I've been having a few nightmares with NSFetchedResultsController and in trying to find a solution to my problem outlined here, I came across something unusual about the data being fetched by a fetchedResultsController, particularly the sectionNameForKeypath
parameter.
Suppose I have an object model with 2 Entities and relationships as per below:
Item <<--> Category
Category has an (NSNumber*) attribute called displayOrder
which is used to separate the fetched Item instances into sections in a table view:
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@"category.displayOrder"
cacheName:nil];
If I wanted to somehow manipulate the output of displayOrder
dynamically, the obvious choice would be to overwrite its getter method and do whatever I want in there. But the problem is, the getter for displayOrder
is never accessed during the fetchRequest or by the fetchedResultsController at all, even though I am specifying it in the sectionNameForKeypath
, AND I can see the fetchedResultsController is using its values to separate and display the sections.
Here's a simple example of an overwritten getter method which never gets called. This was placed inside my Category class:
-(NSNumber*)displayOrder
{
NSLog (@"displayOrder getter called");
NSNumber *displayOrder = [NSNumber numberWithInt:0];
return displayOrder;
}
I've also tried overwriting awakeFromFetch
to no avail (also doesn't get called, but that doesn't surprise me as I'm fetching Item instances and accessing the category and its attributes through the relationships).
So finally my question is: how does the category.displayOrder
values get passed to my fetchedResultsController and is there a way for me to control this output?
Thanks in advance for any ideas you may have!
Rog
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 iOS 3.2 开始,NSFetchedResultsController 尽可能直接从数据库使用由sectionNameKeyPath 标识的字段,而不是为每个项目创建对象。您可以尝试对sectionNameKeyPath使用非持久属性,这(根据文档)将强制它创建对象,但如果只是重命名UITableView的部分,那么最好在中进行映射
tableView:titleForHeaderInSection:
。Since iOS 3.2, NSFetchedResultsController uses the field identified by sectionNameKeyPath directly from the database whenever possible, rather than creating objects for every item. You could try using a non-persistent property for the sectionNameKeyPath, which (according to the documentation) would force it to create the objects, but if it's just a matter of renaming the sections for a UITableView it would be better to do the mapping in
tableView:titleForHeaderInSection:
.