【问题标题】:Swift - array seems empty even after filling it with dataSwift - 即使在填充数据后数组似乎也是空的
【发布时间】:2019-12-20 15:29:12
【问题描述】:

我现在遇到了一个非常奇怪的问题,我不知道为什么会发生这种情况。

首先我从我的 Cloud-FirestoreDB 中检索一些 documents。这工作得很好。

func retrieveUserDataFromDB() -> Void {

    // local mutable "WishList" var
    var wList: [Wish] = [Wish]()

    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.wishListImagesArray.append(UIImage(named: "iconRoundedImage")!)
                    self.wishListTitlesArray.append(listName as! String)
                    // 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.wishListTitlesArray.append(listName as! String)
                    self.wishListImagesArray.append(self.images[listImageIDX as! Int])
                    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() 

            }
        }
    }

    // un-hide the collection view
    self.theCollectionView.isHidden = false

    getWishes()

}

正如你在上面的函数中看到的那样,我也是.append 我的wishListTitlesArray。但是,如果我尝试访问其中的数据,则不会发生任何事情..

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

    var counter = 0
    print("yikes")

    for list in self.wishListTitlesArray {
        print("yeet")
        db.collection("users").document(userID).collection("wishlists").document(list).collection("wünsche").getDocuments() { ( querySnapshot, error) in
            if let error = error {
                print(error.localizedDescription)
            }else{
                var wList = self.userWishListData[counter]
                for document in querySnapshot!.documents {
                    let documentData = document.data()
                    let wishName = documentData["name"]
                    wList.append(Wish(withWishName: wishName as! String, checked: false))
                    counter += 1
                    print("hi")
                    print(wishName as! String)
                }
            }
        }
    }
}

现在它正在打印print("yikes"),所以它甚至没有进入for-loop

我还尝试打印func retrieveUserDataFromDB 末尾的所有元素,但这也无济于事。它没有崩溃或显示任何错误。

可能只是一个愚蠢的错误,但如果有人知道为什么会发生这种情况,我将非常感激!

【问题讨论】:

    标签: ios arrays firebase arraylist google-cloud-firestore


    【解决方案1】:

    我不知道 Firestore 对象,但我认为您的 getDocument 方法是一个异步函数。 所以在执行时间线中,getWishes 在调用 getDocuments 的同时被调用... getDocument 完成后,您应该执行您的函数 getWishes。

    为此,您可以像这样移动代码:

    func retrieveUserDataFromDB() -> Void {
    
    // local mutable "WishList" var
    var wList: [Wish] = [Wish]()
    
    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.wishListImagesArray.append(UIImage(named: "iconRoundedImage")!)
                    self.wishListTitlesArray.append(listName as! String)
                    // 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.wishListTitlesArray.append(listName as! String)
                    self.wishListImagesArray.append(self.images[listImageIDX as! Int])
                    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() 
    
            }
            getWishes()
        }
    }
    
    // un-hide the collection view
    self.theCollectionView.isHidden = false
    

    }

    检查快速关闭 https://docs.swift.org/swift-book/LanguageGuide/Closures.html

    您也可以查看DispatchGroup https://developer.apple.com/documentation/dispatch/dispatchgroup

    【讨论】:

    • 谢谢,原来如此。 getWishes 没有按应有的方式工作,但至少它正在打印所有 print("") -tests ,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2020-07-14
    • 2020-02-13
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多