Assign value of a Firestore document to a variable

后端 未结 1 1839
抹茶落季
抹茶落季 2020-11-29 14:07

I am trying to read the value of a Firestore document. I have tried doing it two different ways, but each fails. In the first one, an error is thrown on the return line:

相关标签:
1条回答
  • 2020-11-29 14:44

    The Firebase call is an asynchronous function. It takes extra time to execute because it's talking to a server (as you've noted) - as a result, the completion block (the block that defines document and err in your example) happens at a different time, outside of the rest of the body of the function. This means you can't return a value from inside it, but you can pass another closure through to it, to execute later. This is called a completion block.

    func readAvailableLists(forUser user: String, completion: @escaping ([String]?, Error?) -> Void) -> [String] {
        let db = Firestore.firestore()
        db.collection("userslist").document(user).getDocument { (document, err) in
            if let document = document, document.exists {
                // We got a document from Firebase. It'd be better to
                // handle the initialization gracefully and report an Error
                // instead of force unwrapping with !
                let strings = (UserInformationDocument(dictionary: document.data()!)?.lists!)!
                completion(strings, nil)
            } else if let error = error {
                // Firebase error ie no internet
                completion(nil, error)
            }
            else { 
                // No error but no document found either
                completion(nil, nil)
            }
        }
    }
    

    You could then call this function elsewhere in your code as so:

    readAvailableLists(forUser: "MyUser", completion: { strings, error in
        if let strings = strings {
            // do stuff with your strings
        }
        else if let error = error {
            // you got an error
        }
    })
    
    0 讨论(0)
提交回复
热议问题