简体   繁体   中英

Swift- Response Handler on Firebase Write Operations

I am following the new Firebase documentation to perform a write operation. However, I don't see any code that would allow me to listen to Firebase's response on a write operation.

Here is my code:

ref.child("Canciones").childByAutoId().setValue(["Categoria":song.categoria, "Titulo":song.titulo, "Autor":song.autor, "Votos":song.votos])

I am looking for something that would return a response for me to inform the user that the request was performed successfully. For example:

ref.child("Canciones").childByAutoId().setValue(["Categoria":song.categoria, "Titulo":song.titulo, "Autor":song.autor, "Votos":song.votos]) { response in
if error != nil {
//..etc..//
}

Thanks!

let message = ["name": "puf", "text": "Hello from iOS"]
ref!.childByAutoId().setValue(message) { (error) in
    print("Error while writing message \(error)")
}

From the documentation and as an alternative answer

func setValue(_ value: Any?, withCompletionBlock block: @escaping (Error?, FIRDatabaseReference) -> Void)

and used like this

let ref = rootRef.child("Canciones").childByAutoId()    
ref.setValue("Hello, World", withCompletionBlock: { (error, ref) in
        if error == nil {
            print("success")
        } else {
            print("failure")
        }
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM