2

I need a bit of words about this thing, i know, sure there is something that I'm missing here.

Why avatarUsername and userName into the return are nil?, but inside the user.getDocument {} are having a value... what I'm missing there?

func userData(uID: String) -> (avatar: String, username: String){

    let user = db.collection("users").document(uID)
    var userName : String!
    var avatarUsername : String!

    user.getDocument {(document, error) in

        if let document = document, document.exists {
            guard let avatar = document.data()!["avatar"] else{
                return
            }

            avatarUsername = (avatar as? String)!

            guard let username = document.data()!["username"] else{
                return
            }

            userName = (username as? String)!

        }


        //print("username \(String(describing: userName!))")

    }
    //print("username \(String(describing: userName!))")
    return (avatarUsername,userName)

}

Thanks!

Asinox
  • 6,747
  • 14
  • 61
  • 89
  • You never assigned anything to `avatarUsername` and `userName`. `user.getDocument` is async and is not guaranteed to finish executing before you return from the function. You need to move your return statement inside the `getDocument` block. – Vahid Amiri Jun 10 '18 at 03:42

0 Answers0