如何通过Desc SwiftUI对Firestore数据进行云排序 [英] How to Cloud Firestore Data Sort by Desc SwiftUI
问题描述
我正在尝试从我的 Firestore
集合中获取数据,现在我要做的是按"dateCode"字段之一对文档进行排序.附上以下代码:
I am trying to fetch the data from my Firestore
collection and now what i want to do is to sort the documents by one of the fields say "dateCode" Attaching codes below:
func getDescOrderData(completion: @escaping ([String:Any]) -> Void) {
var dictInfo = [String:Any]()
Firestore..collection("OldData").order(by: "dateCode", descending: true)
.whereField("dateCode", isLessThanOrEqualTo: "Mon")
.whereField("studentId", isEqualTo: self.studentId)
.getDocuments { (querySnapshot, error) in
if let error = error {
print("Old History Data error: \(error.localizedDescription)")
return
}
if let snapshot = querySnapshot {
for document in snapshot.documents {
dictInfo.append(document.data())
}
completion(dictInfo)
}
}
}
但是我在下面给出了错误:-
But i got error given below:-
旧历史记录数据错误
:查询需要索引.您可以在这里创建它:https://console.firebase.googl .................
Old History Data error
: The query requires an index. You can create it here: https://console.firebase.googl.................
任何帮助将不胜感激.
推荐答案
代码正在尝试使用需要文档,该链接应该可以这是错误消息中的内容:
The code is trying to use query which needs so called Composite Index. Actually according to the documentation it should be possible by the link that is in the error massage:
如果您尝试使用不映射的range子句进行复合查询到现有索引,您将收到一个错误.错误消息包括在Firebase控制台中创建丢失索引的直接链接.
If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.
只需在浏览器中打开它并创建索引.就这样!:)
Just open it in browser and create the index. That's all! :)
这篇关于如何通过Desc SwiftUI对Firestore数据进行云排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!