使用NSFetchedResultsController和sectionNameKeyPath是关系时如何获取节名称?
在以下代码中:
NSFetchedResultsController *frc =
[[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:myManagedObjectContext
sectionNameKeyPath:@"store"
cacheName:@"SomeCache"
];
sectionNameKeyPath 的 @"store" 值实际上指向与 Store 实体的关系,该实体具有我真正需要的名称属性作为我的节标题标题。
但是我的代码设置方式无法完成,因为我得到的部分标题如下: 0x5b504f0 0x5b51190 据我所知,哪些是正在获取的 Store 对象的代码数据地址。
我如何使用 NSFetchedResultsController 以便我可以告诉它我希望它获取从sectionNameKeyPath:@"store" 获取的任何内容的 name 属性?或者还有其他解决方法吗?
In the following code:
NSFetchedResultsController *frc =
[[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:myManagedObjectContext
sectionNameKeyPath:@"store"
cacheName:@"SomeCache"
];
The @"store" value for sectionNameKeyPath actually points to a relationship to a Store entity which has a name attribute that I really need as my section header title.
But the way my code is setup it cannot be accomplished as I instead get section titles like:
0x5b504f0
0x5b51190
Which are code data addresses for the Store object being fetched as far as I can tell.
How can I use NSFetchedResultsController so that I may tell it that I want it to fetch the name attribute of whatever it fetches from sectionNameKeyPath:@"store" ? Or is there some other workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
sectionNameKeyPath:@"store.name"
Try
sectionNameKeyPath:@"store.name"
我知道这是一个旧线程,但我想使用 Swift 添加我的解决方案。
我必须建议,此解决方案取决于 NSFetchedResultsController 返回的节名称的格式,因此它取决于 Apple 可以更改的内部实现。
部分名称包含持久数据存储中对象 ID 的 URI,因此您可以通过先提取 URI,然后向存储请求具有相应 URI 的对象来获取对象。
那么,您需要检查是否有错误(我会在那些
let
前面使用很多guard
),但你明白了。I know this is an old thread, but I would like to add my solution using Swift.
I must advice that this solution depends on the format of the section name returned by the
NSFetchedResultsController
, so it depends on internal implementations that can be changed by Apple.The section name contains the URI of the object ID in the Persistent Data Store, so you can get the object by first extracting the URI and then asking the Store for the object with the corresponding URI
Well, you need to check for errors (I would use a lot of
guard
in front of thoselet
) but you get the idea.