0

I am having a really weird problem at the moment and I have no clue why this happens.

First I retrieve some documents from my Cloud-FirestoreDB. This works perfectly fine.

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()

}

As you can see in the function above I also .append my wishListTitlesArray. However if I try to access the data inside of it nothing happens..

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)
                }
            }
        }
    }
}

Right now it is printing the print("yikes") so it is not even going inside the for-loop.

I also tried printing all the elements in the end of func retrieveUserDataFromDB but that doesn't doing anything either. It is not crashing or showing any error.

Probably just a stupid mistake but if anyone has any idea why this happens I would be very grateful!

1 Answer 1

1

I don't know Firestore object, but I think your getDocument method is an async func. So in the execution timeline, getWishes is called in the same time you called getDocuments... You should execute your fonction getWishes when getDocument is done.

To do it you can move your code like this:

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

}

Check swift closures https://docs.swift.org/swift-book/LanguageGuide/Closures.html

You can also check DispatchGroup https://developer.apple.com/documentation/dispatch/dispatchgroup

Sign up to request clarification or add additional context in comments.

1 Comment

thanks that was actually it. getWishes doesn't work the way it should but at least it is printing all the print("") -tests , thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.