简体   繁体   English

Firebase写操作上的Swift响应处理程序

[英]Swift- Response Handler on Firebase Write Operations

I am following the new Firebase documentation to perform a write operation. 我正在遵循新的Firebase文档来执行写操作。 However, I don't see any code that would allow me to listen to Firebase's response on a write operation. 但是,我看不到任何允许我在写操作中侦听Firebase响应的代码。

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")
        }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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