ios - Swift - Firebase - 订单收集

标签 ios swift firebase google-cloud-firestore

我正在尝试检索一些文档,但我需要按我的“愿望 list ”- 集合 中的某些数据(“ListIDX”)对它们进行排序。

我试过了,但这是不允许的:

db.collection("users").document(userID).collection("wishlists").order(by: "ListIDX").document(list.name).collection("wünsche"). getDocuments()

这是我的函数:

    func getWishes (){
            let db = Firestore.firestore()
            let userID = Auth.auth().currentUser!.uid

            var counter = 0

            for list in self.dataSourceArray {
print(list.name) // -> right order
                db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").getDocuments() { ( querySnapshot, error) in
print(list.name) // wrong order
                    if let error = error {
                        print(error.localizedDescription)
                    }else{
                        // DMAG - create a new Wish array
                        var wList: [Wish] = [Wish]()
                        for document in querySnapshot!.documents {
                            let documentData = document.data()
                            let wishName = documentData["name"]
                            wList.append(Wish(withWishName: wishName as! String, checked: false))
                        }
                        // DMAG - set the array of wishes to the userWishListData
                        self.dataSourceArray[counter].wishData = wList
                        counter += 1
                    }
                }
            }
        }

这是我最终真正想要实现的:

self.dataSourceArray[ListIDX].wishData = wList

更新

我还有一个函数可以按正确的顺序检索我的愿望 list 。也许我可以在其中添加 getWishes,这样它的顺序也是正确的。

    func retrieveUserDataFromDB() -> Void {

        let db = Firestore.firestore()
        let userID = Auth.auth().currentUser!.uid
        db.collection("users").document(userID).collection("wishlists").order(by: "listIDX").getDocuments() { ( querySnapshot, error) in
            if let error = error {
                print(error.localizedDescription)
            }else {
                // get all documents from "wishlists"-collection and save attributes
                for document in querySnapshot!.documents {
                    let documentData = document.data()
                    let listName = documentData["name"]
                    let listImageIDX = documentData["imageIDX"]

                    // if-case for Main Wishlist
                    if listImageIDX as? Int == nil {
                        self.dataSourceArray.append(Wishlist(name: listName as! String, image: UIImage(named: "iconRoundedImage")!, wishData: [Wish]()))
                        // set the drop down menu's options
                        self.dropDownButton.dropView.dropDownOptions.append(listName as! String)
                        self.dropDownButton.dropView.dropDownListImages.append(UIImage(named: "iconRoundedImage")!)
                    }else {

                        self.dataSourceArray.append(Wishlist(name: listName as! String, image: self.images[listImageIDX as! Int], wishData: [Wish]()))

                        self.dropDownButton.dropView.dropDownOptions.append(listName as! String)
                        self.dropDownButton.dropView.dropDownListImages.append(self.images[listImageIDX as! Int])
                    }

//                    // create an empty wishlist
//                    wList = [Wish]()
//                    self.userWishListData.append(wList)

                    // reload collectionView and tableView
                    self.theCollectionView.reloadData()
                    self.dropDownButton.dropView.tableView.reloadData()

                }

            }
            self.getWishes()
        }

为了更好地理解:

git repo

最佳答案

正如@algrid 所说,如果您要使用 list.name 获取特定元素,则使用 order()collection 进行排序是没有意义的 最后,而不是第一个或最后一个。我建议将您的代码更改为:

db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").getDocuments()

关于ios - Swift - Firebase - 订单收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59556505/

相关文章:

javascript - React BootstrapTable如何动态为firebase数据创建行

java - Android - Firebase OrderByChild 使应用程序崩溃

ios - 字典错误 : Ambiguous reference to member '+'

ios - 在 Storyboard中的模式堆栈中取消/委托(delegate)第二个 View

ios - AppDelegate问题

ios - 如何用单个表格部分显示不同的 View ?

javascript - 发出多个 Axios 请求导致 CORS 错误

ios - 重构swift代码

ios - 如何在 CoreMotion 框架中找到最小和最大频率间隔

ios - Swift 中的向下钻取 TableView