本文介绍了在不同的字段Swift Firestore上使用范围过滤器按时间戳排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题,但尚未找到可行的解决方案.我想要的是获取一个年龄范围内的用户列表,并按"createdAt"时间戳排序,但是由于生日"范围过滤器,因此第一个orderBy必须位于生日"字段中.

I have asked this question before but not found a working solution yet.What I want is to get a list of users within an age range and ordered by the 'createdAt' timestamp, but because of the 'birthday' range filter the first orderBy must be on 'birthday' field.

这是我的查询:

database.collection("users")
    .whereField("birthday", isGreaterThan: exploreSettings.upperAgeDate)
    .whereField("birthday", isLessThan: exploreSettings.lowerAgeDate)
    .whereField("isHidden", isEqualTo: false)
    .order(by: "birthday", descending: true)
    .order(by: "createdAt", descending: true)
    .limit(to: 8)
    .start(afterDocument: lastSnapshot)
    .getDocuments {...}

这就是它将返回的内容

[
    ["name": "John", "age": 21, "createdAt": 2005],
    ["name": "Jane", "age": 22, "createdAt": 2003],
    ["name": "Adam", "age": 23, "createdAt": 2004],
    ["name": "Karl", "age": 24, "createdAt": 2001],
    ["name": "Mila", "age": 25, "createdAt": 2002],
]

我想要它返回什么

[
    ["name": "Karl", "age": 24, "createdAt": 2001],
    ["name": "Mila", "age": 25, "createdAt": 2002],
    ["name": "Jane", "age": 22, "createdAt": 2003],
    ["name": "Adam", "age": 23, "createdAt": 2004],
    ["name": "John", "age": 21, "createdAt": 2005],
]

有人建议我以某种方式合并生日并在单个字段中创建时间戳记,但我知道我应该如何做到这一点

Some suggested that I somehow merged the birthday and created timestamp into a single field but I have know ideer how I should be able to do that

推荐答案

尝试使用此方法:

      let arrResponse = [
        ["name": "John", "age": 21, "createdAt": 2005],
        ["name": "Jane", "age": 22, "createdAt": 2003],
        ["name": "Adam", "age": 23, "createdAt": 2004],
        ["name": "Karl", "age": 24, "createdAt": 2001],
        ["name": "Mila", "age": 25, "createdAt": 2002],
        ]


    let descriptor : NSSortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)

    let sortedResult = (arrResponse as NSArray).sortedArray(using: [descriptor])

    print(sortedResult)

输出如下:

这篇关于在不同的字段Swift Firestore上使用范围过滤器按时间戳排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:29