0

I'm trying to return a list of booleans to check in what registration step the user is and if it has registered through Facebook or Phone. The function I made gets the user doc and checks for two fields to see if they exist (interest and id). id, because only FB users have that field and interests because that is the last step of the registration process. It appears the function is returning the default values [true,true] before I even get the chance to modify the values.

fileprivate func checkRegistrationComplete(_ uid: String?) -> [Bool] {
    //I will return a list with the first bool to know if it has filled all the info and the second if it has done so through FB or phone
    var userRegistered = true
    var userRegisteredFB = true
    let db = Firestore.firestore()
    let docRef = db.collection("users").document(uid!)
    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data()
            userRegistered = dataDescription?["interest"] != nil
            userRegisteredFB = dataDescription?["id"] != nil
            print(userRegisteredFB)
            print(userRegistered)
        } else {
            print("Document does not exist")
        }
    }
    print(userRegisteredFB,userRegistered)
    return [userRegistered,userRegisteredFB]
}

The output should be [false,true] but it returns [true,true]. And the print functions show that the get method is actually working but it shows after the return call

  • 1
    The data is loaded from Firestore asynchronously, so by the time you print/return the value, the closure hasn't run yet. See https://stackoverflow.com/questions/31878929/how-do-i-access-variables-that-are-inside-closures-in-swift, https://stackoverflow.com/questions/43823808/access-firebase-variable-outside-closure, https://stackoverflow.com/questions/48875822/how-do-i-pull-this-value-out-of-a-closure-to-update-a-cutom-object-in-swift, https://stackoverflow.com/questions/52244924/ – Frank van Puffelen Feb 15 '19 at 23:34
  • Also, Swift supports tuples. You shouldn't return arrays of unnamed elements. Use a tuple instead. – Alexander Feb 15 '19 at 23:36
  • Possible duplicate of [getting data out of a closure that retrieves data from firebase](https://stackoverflow.com/questions/38364288/getting-data-out-of-a-closure-that-retrieves-data-from-firebase) – Frank van Puffelen Feb 15 '19 at 23:37

0 Answers0