使用NSFetchedResultsController和sectionNameKeyPath是关系时如何获取节名称?

发布于 2024-12-02 14:05:59 字数 536 浏览 2 评论 0原文

在以下代码中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉徒眸意 2024-12-09 14:05:59

尝试 sectionNameKeyPath:@"store.name"

Try sectionNameKeyPath:@"store.name"

一个人练习一个人 2024-12-09 14:05:59

我知道这是一个旧线程,但我想使用 Swift 添加我的解决方案。

我必须建议,此解决方案取决于 NSFetchedResultsController 返回的节名称的格式,因此它取决于 Apple 可以更改的内部实现。

部分名称包含持久数据存储中对象 ID 的 URI,因此您可以通过先提取 URI,然后向存储请求具有相应 URI 的对象来获取对象。

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let sectionInfo = fetchedResultsController?.sections![section]
    let sectionName = sectionInfo?.name

    //This part depends on the internal implementation of sectionInfo.name
    let initialRange = sectionName?.rangeOfString("<")
    let finalRange = sectionName?.rangeOfString(">")

    let uri = sectionName?.substringWithRange((initialRange?.endIndex)!..<(finalRange?.startIndex)!)
    let url = NSURL(string: uri!)

    let store = fetchedResultsController?.managedObjectContext.persistentStoreCoordinator

    let objectID = store?.managedObjectIDForURIRepresentation(url!)
    let coreObject = fetchedResultsController?.managedObjectContext.objectWithID(objectID!)
    //return the correct property from the object as the title
    let title = ...
    return title
}

那么,您需要检查是否有错误(我会在那些 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

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let sectionInfo = fetchedResultsController?.sections![section]
    let sectionName = sectionInfo?.name

    //This part depends on the internal implementation of sectionInfo.name
    let initialRange = sectionName?.rangeOfString("<")
    let finalRange = sectionName?.rangeOfString(">")

    let uri = sectionName?.substringWithRange((initialRange?.endIndex)!..<(finalRange?.startIndex)!)
    let url = NSURL(string: uri!)

    let store = fetchedResultsController?.managedObjectContext.persistentStoreCoordinator

    let objectID = store?.managedObjectIDForURIRepresentation(url!)
    let coreObject = fetchedResultsController?.managedObjectContext.objectWithID(objectID!)
    //return the correct property from the object as the title
    let title = ...
    return title
}

Well, you need to check for errors (I would use a lot of guard in front of those let) but you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文