热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

检查用户是否存在firebase3.0+swift-Checkifuserexistwithfirebase3.0+swift

IhaveaappthataftertheuseruseFirebaseauthitstorethedataontheFirebasedatabase.Befor

I have a app that after the user use Firebase auth it store the data on the Firebase database. Before storing the data, I want to check if the username the user give already exist in the database. So if it not exist I could give the user this unique username(like every user have a unique username). So I have a textField where the user enter his username, and then press Next. Then the app should check if the username exist or not, and tell the user if he need to change it.

我有一个应用,在用户使用Firebase身份验证后,它会将数据存储在Firebase数据库中。在存储数据之前,我想检查用户提供的用户名是否已存在于数据库中。因此,如果它不存在,我可以给用户这个唯一的用户名(就像每个用户都有一个唯一的用户名)。所以我有一个textField,用户输入他的用户名,然后按Next。然后应用程序应检查用户名是否存在,并告诉用户是否需要更改用户名。

So the code I used to check if the username exist:

所以我用来检查用户名是否存在的代码:

        let databaseRef = FIRDatabase.database().reference()

        databaseRef.child("Users").observeSingleEventOfType(.Value, withBlock: { (snapshot) in

            if snapshot.hasChild(self.usernameTextField.text!){

                print("user exist")

            }else{

                print("user doesn't exist")
            }
        })  

So every time the next button is pressed, this code is called. The problem with this is that the result always remain the same as the first search (even after the textField value change). For example, if I search Jose, and Jose exist in my database so is going to print "user exist". But when I change the textField to name that don't exist, it still show "user exist".

因此,每次按下下一个按钮,都会调用此代码。这样的问题是结果始终与第一次搜索保持相同(即使在textField值更改之后)。例如,如果我搜索Jose,并且Jose存在于我的数据库中,那么将打印“user exists”。但是当我将textField更改为不存在的名称时,它仍然显示“用户存在”。

2 个解决方案

#1


4  

I figured out I need to change the .Value to FIRDataEventType.Value

我想我需要将.Value更改为FIRDataEventType.Value

 if (usernameTextField.text?.isEmpty == false){
        let databaseRef = FIRDatabase.database().reference()

         databaseRef.child("Users").observeSingleEventOfType(FIRDataEventType.Value, withBlock: { (snapshot) in

            if snapshot.hasChild(self.usernameTextField.text!){

                print("true rooms exist")

            }else{

                print("false room doesn't exist")
            }


        })

#2


0  

struct ModelUser {
    var id: String
    var name: String

    init(data: DataSnapshot) {
        // do init stuff
    }
}

func isUserRegistered(with id: String, completion: @escaping (_ exists: Bool, _ user: ModelUser?) -> ()) {
    DatabaseReference.users.child(id).observeSingleEvent(of: .value) { (snapshot) in
        if snapshot.exists() {
            // user is already in our database
            completion(true, ModelUser(data: snapshot))
        } else {
            // not in database
            completion(false, nil)
        }
    }
}

This worked for me in a similar situation as yours. You can also go the Rx way like this.

这对我来说和你的情况类似。你也可以像这样走Rx。

enum CustomError: Error {
    case userNotRegistered

    var localizedDescription: String {
        switch self {
        case .userNotRegistered:
            return "Dude is not registered..."
        }
    }
}

func isUserRegistered(with id: String) -> Observable<(ModelUser)> {
    let reference = DatabaseReference.users.child(id)

    return Observable.create({ observer -> Disposable in
        let listener = reference.observe(.value, with: { snapshot in
            if snapshot.exists() {
                observer.onNext(ModelUser(data: snapshot))
                observer.onCompleted()
            } else {
                observer.onError(CustomError.userNotRegistered)
            }
        })
        return Disposables.create {
            reference.removeObserver(withHandle: listener)
        }
    })
}

The key in both cases is using the .exists() method of the snapshot.

两种情况下的关键是使用快照的.exists()方法。


推荐阅读
author-avatar
郑小斌-杭州
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有