1

I'm trying to create an array of structs by creating a function to fetch data from firestore and then passing the three structs fetched to an array of structs. Here is my code:

func fetchUsers() {

    var user1: User
    var user2: User
    var user3: User

    docRef = Firestore.firestore().document("users/user1")
    docRef.getDocument { (docSnapshot, error) in
        guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
        let myData = docSnapshot.data()
        let fName = myData!["name"] as? String ?? ""
        let fUsername = myData!["username"] as? String ?? ""
        let fBioText = myData!["bioText"] as? String ?? ""

        let user = User(name: fName, username: fUsername, bioText: fBioText, profileImage: #imageLiteral(resourceName: "mauricioprofileimage"))

        user1 = user
    }

    self.docRef = Firestore.firestore().document("users/user2")
        self.docRef.getDocument { (docSnapshot, error) in
        guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
        let myData = docSnapshot.data()
        let fName = myData!["name"] as? String ?? ""
        let fUsername = myData!["username"] as? String ?? ""
        let fBioText = myData!["bioText"] as? String ?? ""

        let user = User(name: fName, username: fUsername, bioText: fBioText, profileImage: #imageLiteral(resourceName: "trumpprofileimage"))

        user2 = user
    }

    self.docRef = Firestore.firestore().document("users/user1")
        self.docRef.getDocument { (docSnapshot, error) in
        guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
        let myData = docSnapshot.data()
        let fName = myData!["name"] as? String ?? ""
        let fUsername = myData!["username"] as? String ?? ""
        let fBioText = myData!["bioText"] as? String ?? ""

        let user = User(name: fName, username: fUsername, bioText: fBioText, profileImage: #imageLiteral(resourceName: "amandaprofileimage"))

        user3 = user
    }

    return [user1, user2, user3]
}

let users: [User] = fetchUsers()

The problem I'm getting is that when I try to fill my users array, it says that it doesn't recognize the user1, user2 and user3 that I created in the Fetchfuncion.

Ps: I'm using firestore. Thank you for all the help!

3
  • 1
    What's the actual question here? Commented Jan 26, 2018 at 21:26
  • I don't understand what the question is, either. Commented Jan 26, 2018 at 23:50
  • I edited the question in the end Commented Jan 27, 2018 at 2:55

1 Answer 1

1

Put each request in a group, and only once all three users have been retrieved, return the array of users. What's happening with your code is that it's returning an array of empty users (or perhaps 1-3 inconsistently), due to the fact that these calls are asynchronous so the data returned from a firebase request isn't guaranteed to exist when the array is returned.

let group = DispatchGroup()

group.enter()
make_async_request_1 {
    // Process response
    group.leave()
}

group.enter()
make_async_request_2 {
    // Process response
    group.leave()
}

group.enter()
make_async_request_3 {
    // Process response
    group.leave()
}
group.notify(queue: .main) {
    // This will run after all 3 group.leave() calls are made
}
Sign up to request clarification or add additional context in comments.

4 Comments

@MauricioTakashiKiyama its for anything that is asynchronous, I use this a lot :)
@Jay make_async_request_1 is getting the errors "Use of unresolved identifier "make_async_request_1" and 2 and 3 as well.
@MauricioTakashiKiyama that's because those are placeholders, they aren't literal swift code. Those would be the functions you want to execute asynchronously.
@MauricioTakashiKiyama did the answer work? If so please mark my answer as correct

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.