不同字段上带范围过滤器的按时间戳订购Swift Firestore
我以前问过这个问题,但还没有找到有效的解决办法。 我想要的是获得一个年龄范围内的用户列表,并按“createdAt”时间戳排序,但由于“生日”范围筛选器,第一个orderBy必须位于“生日”字段中 这是我的疑问:不同字段上带范围过滤器的按时间戳订购Swift Firestore,swift,firebase,google-cloud-firestore,Swift,Firebase,Google Cloud Firestore,我以前问过这个问题,但还没有找到有效的解决办法。 我想要的是获得一个年龄范围内的用户列表,并按“createdAt”时间戳排序,但由于“生日”范围筛选器,第一个orderBy必须位于“生日”字段中 这是我的疑问: database.collection("users") .whereField("birthday", isGreaterThan: exploreSettings.upperAgeDate) .whereField("birthday", isLessThan: e
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],
]
有人建议我以某种方式将生日和创建的时间戳合并到一个字段中,但我知道我应该如何做到这一点
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)
输出如下:
您想使用createdAt对数组进行排序。我想填充createdAt@abhirajsinhthakore排序的数组。我在您的数组中看到,createdAt您没有键值对。?这是你的回答吗。?或者在打印时出现了一些错误。?我可以用“CreatedAt”键对数组进行排序,但前提是在created at中有一个键值对。很抱歉,这是堆栈溢出中的一个输入错误。这些数组只是查询将从db获得什么以及et应该获得什么的示例@Abhirajsinhthakore添加了带有编辑和输出的答案,请检查它是否适用于您。?是的,这将对数组进行排序,但不是我想要的。我需要通过firestore查询直接从数据库中获取年龄范围内的最新用户。我的方法是按原样获取数据,然后按此排序,这样在任何时候都可以从顶部获取最新数据。这将是一个有效的解决方案,但是,由于我正在使用限制和分页,在向下滚动时,下一次获取可能包含较新的用户。我可以知道您的查询不起作用的原因吗?因为查询中的“.order(by:“birth”,descending:true)”行。