Firebase query sort order in swift?

前端 未结 4 2193
野的像风
野的像风 2020-12-18 13:32

When I load the following Firebase Database data into my tableView, the data is sorted in ascending order by date. How can I order this by descending (show the newest post a

4条回答
  •  囚心锁ツ
    2020-12-18 14:17

    Swift 5: Add reversed() to your objects after sorting by the required field. For example, let's assume you have a day of the month in the "day" field in FireStore. Something like this will do the trick (call loadData() function in viewDidLoad to see the output):

    let db = Firestore.firestore()
    func loadData() {
            db.collection("FireStoreCollectionName").order(by: "day").getDocuments { (querySnapshot, error) in
                if let e = error {
                    print("There was an issue retrieving data from Firestore, \(e)")
                } else {
                    for document in querySnapshot!.documents.reversed() {
                        let data = document.data()
                        let fDay = data["day"] as! Int
                        print(fDay)                                       
                    }
                }
            }
        }
    

提交回复
热议问题