ios - 从 CoreData 上的不同属性获取前 1 个

标签 ios iphone core-data ios8 ios9

您好,我有一个格式如下所示的表格。

我想构建一个历史 View ,因此我需要来自不同用户的最新消息,按时间戳排序!

+---+--------------------+--------------------+----- ----------+ | |用户名 |留言 |时间戳| +---+--------------------+--------------------+----- ----------+ | 1 |约翰 |你好 | 486380161.723 | | 2 |马克|电子表格 | 486380264.723 | | 3 |约翰 |仅供引用| 486380366.723 | | 4 |约翰 |再见| 486557497.271 | | 5 |马克|你好吗? | 486557597.274 | | 6 |马里奥|什么? | 486558597.274 | +---+--------------------+--------------------+----- ----------+

这就是我应该得到的结果。

+---+--------------------+--------------------+----- ----------+ | |用户名 |留言 |时间戳| +---+--------------------+--------------------+----- ----------+ | 6 |马里奥|什么? | 486558597.274 | | 5 |马克|你好吗? | 486557597.274 | | 4 |约翰 |再见| 486557497.271 | +---+--------------------+--------------------+----- ----------+

目前,我正在获取所有不同的用户名,迭代每个用户名并查询该用户名的消息,按时间戳排序,并使用limit(1)

我对这个解决方案不满意,所以有人可以帮助我找到更好的解决方案吗?

谢谢, 马里奥

最佳答案

可以通过两次获取来完成此操作,但有一个警告,我会在执行时提到。

第一次获取获取用户名和每个用户名的最新时间戳:

    let maxTimestampRequest = NSFetchRequest(entityName: "Entity")
    maxTimestampRequest.resultType = .DictionaryResultType

    let maxTimestampExpression = NSExpression(format: "max:(timestamp)")
    let maxTimestampExpressiondescription = NSExpressionDescription()
    maxTimestampExpressiondescription.name = "maxTimestamp"
    maxTimestampExpressiondescription.expression = maxTimestampExpression
    maxTimestampExpressiondescription.expressionResultType = .DoubleAttributeType

    maxTimestampRequest.propertiesToFetch = ["username", maxTimestampExpressiondescription]
    maxTimestampRequest.propertiesToGroupBy = ["username"]

执行该提取,您将获得一个字典数组。每个字典都包含一个用户名和该用户名的最新时间戳:

Optional([{
    maxTimestamp = "486557497.271";
    username = John;
}, {
    maxTimestamp = "486558597.274";
    username = Mario;
}, {
    maxTimestamp = "486557597.274";
    username = Mark;
}])

获取完整记录需要第二次获取。如果先前获取的结果位于名为 results 的数组中,

    var predicates = [NSPredicate]()
    for maxTimestampInfo in results! {
        let username = maxTimestampInfo["username"]!
        let timestamp = maxTimestampInfo["maxTimestamp"]!
        let partialPredicate = NSPredicate(format: "username=%@ and timestamp=%@", argumentArray:[ username, timestamp ])
        predicates.append(partialPredicate)
    }
    let completePredicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates)

    let fetch = NSFetchRequest(entityName: "Entity")
    fetch.predicate = completePredicate

执行提取操作,您将获得符合您要求的完整托管对象。

需要注意的是,第二次提取中的谓词可能会非常大,具体取决于您拥有的用户数量。

关于ios - 从 CoreData 上的不同属性获取前 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37730879/

相关文章:

ios - 方向横向 -> 纵向不起作用

iphone - 在 View 弹出之前存储 UITextField 内容

iphone - 如何以编程方式从 textview 中关闭键盘

cocoa - 一键发送多个操作?

ios - NSFetchedResultsController 委托(delegate)仅在首次启动应用程序时触发方法

objective-c - 如何使复选框单元格中的文本能够被编辑?

ios - 展开/折叠 UITableview

ios - 使用 Bing API,如何实现与 Bing iOS 应用程序相同的圆柱 View ?

iphone - 在我的邮件中附加 PDF/Doc 文件

iPhone 开发 - XMLParser 与 libxml2 与 TouchXML