1

I have been having some trouble creating a temporary array of user data from Firestore. Basically I created a function that retrieves user data from a Firestore collection and then iterates through each document within that collection, creating an instance of my "Thought" struct for each one. I then append each "Thought" instance to a temporary array called "tempThoughts", and the function then returns that array. The problem is that nothing seems to be appended to the array in the function. When I test it by printing out the contents of the array upon completion, it just prints an empty array.

The data itself is being read from the Firestore collection as it prints out each document the function iterates through, so I don't think that is the problem. I also tried checking to see if I am actually creating instances of the "Thought" struct properly by printing that out, and that seemed to be working. Does anyone have any idea what's wrong with the way I am appending the struct instances to the array? Perhaps there is a better way to go about doing this? Thanks for any help in advance.

Here is my current function:

func getUserDocuments() -> [Thought]{
var tempThoughts = [Thought]()
db.collection(cUser!.uid).getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in querySnapshot!.documents {
            print("\(document.documentID) => \(document.data())")
            let tempThought: Thought = Thought(id: document.get("id") as! String, content: document.get("content") as! String, dateCreated: document.get("timestamp") as! String, isFavorite: (document.get("isFavorite") != nil))

            tempThoughts.append(tempThought)
        }
    }
}
print("TEST")
print(tempThoughts)
return tempThoughts

}

2 Answers 2

1

Your getDocuments is an asynchronous operation. And you've updated your tempThoughts in it's completion only. But the place where you've printed it out will get executed before the getDocuments completion. Check out the order of results logged in the console.

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

2 Comments

Yeah that was it. I tried printing it out within the else {} part of the operation and the array actually wasn't empty. Thank you!
You can accept the answer if you found it helpful. Cheers.
0

You need to update your code like this

    func getUserDocuments(_ onSuccess: ((_ thoughts: [Thought] ) -> Void), onFailuer: ((_ error: String) -> Void)) {

    var tempThoughts = [Thought]()
    db.collection(cUser!.uid).getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
            onFailuer(err)
        } else {
            DispatchQueue.main.async {
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")
                    let tempThought: Thought = Thought(id: document.get("id") as! String, content: document.get("content") as! String, dateCreated: document.get("timestamp") as! String, isFavorite: (document.get("isFavorite") != nil))

                    tempThoughts.append(tempThought)
                }
                print("TEST")
                print(tempThoughts)
                onSuccess(tempThoughts)
            }
        }
    }
}

user this code

And you can use this function like this

    getUserDocuments({ (thoughts) in
        // Your logic
    }) { (error) in
        // error Occured
    }

2 Comments

I tried this block of code and xCode complained with "Unexpected non-void return value in void function" with each return statement. Any idea why?
@sammcode i have updated my code. as your code can also contain error so you need check for error too

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.