从Firebase数据库异步方法返回值 [英] Return value from Firebase Database async method

查看:95
本文介绍了从Firebase数据库异步方法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查Firebase中是否已经有一个具有所选用户名的用户,并且我已经创建了一个执行此功能的函数checkUsernameAlreadyTaken(username: String) -> Bool. 这是该函数的代码:

I want to check if there is already a user with the chosen username in Firebase and I've created a function checkUsernameAlreadyTaken(username: String) -> Bool that do this. Here is the code pf the function:

func checkUsernameAlreadyTaken(username: String) -> Bool {
    databaseRef.child("usernames").child("\(username)").observe(.value, with: { (snapshot) in
        print(username)
        if snapshot.exists() {
            print("Snapshot exist")
            self.alreadyTaken = true
        }
    })
    if alreadyTaken == true {
        print("Username already taken")
        return false
    }
    else {
        return true
    }

}

问题在于方法observe(_ eventType: FIRDataEventType, with block: (FIRDataSnapshot) -> Void) -> Uint是异步方法,因此我无法使用上面看到的策略.但是我无法从Firebase方法返回值,因为它是空方法...
我该如何解决这个问题?

The problem is that the method observe(_ eventType: FIRDataEventType, with block: (FIRDataSnapshot) -> Void) -> Uint is an async method and so I can not use the strategy you can see above. But I can't return the value from the Firebase method because it's a void method...
How can I solve this problem?

还有一件事.如果连接错误或与服务器之间没有连接,如何也返回false?

One more thing. How can I return false also if there is a connection error or no connection with the server?

推荐答案

您必须自己使用异步完成处理程序,并验证是否存在Internet连接:

You have to employ asynchronous completion handler yourself and verify if there is Internet connection:

func checkUsernameAlreadyTaken(username: String, completionHandler: (Bool) -> ()) {
    databaseRef.child("usernames").child("\(username)").observe(.value, with: { (snapshot) in
        print(username)
        if snapshot.exists() {
            completionHandler(false)
        } else {
            let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")
            connectedRef.observe(.value, with: { snapshot in
                if let connected = snapshot.value as? Bool, connected {
                    completionHandler(true)
                } else {
                    completionHandler(false)
                    // Show a popup with the description
                    let alert = UIAlertController(title: NSLocalizedString("No connection", comment: "Title Internet connection error"), message: NSLocalizedString("No internet connection, please go online", comment: "Internet connection error saving/retriving data in Firebase Database"), preferredStyle: .alert)
                    let defaultOkAction = UIAlertAction(title: NSLocalizedString("No internet connection, please go online", comment: "Internet connection error saving/retriving data in Firebase Database"), style: .default, handler: nil)
                    alert.addAction(defaultOkAction)

                    self.present(alert, animated: true, completion: nil)
                }
            })
        }
    })
}

然后您使用以下方法调用方法:

Then you call your method with:

checkIfUserExists(username: text, completionHandler: { (value) in
    // ...
})

这篇关于从Firebase数据库异步方法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆