Firebase查询唯一的用户名swift [英] Firebase querying for unique Username swift

查看:128
本文介绍了Firebase查询唯一的用户名swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个问题,但他们没有一个能为我工作的答案。



我想这样做,当用户注册一个帐户,它会检查,看他们已经输入的用户名是否已经存在,然后创建帐户。我已经尝试在firebase中使用查询,但我似乎无法得到它的工作。



以下是我的firebase数据的图像:我的firebase数据树



我将使用查询来查找关键字username的字符串吗?

解决方案

你可以这样做,完成块来检查用户名已经存在于您的Firebase数据库中,并基于它创建新用户



$ func checkUserNameAlreadyExist(newUserName: String,completion:@escaping(Bool) - > Void){

let ref = FIRDatabase.database()。reference()
ref.child(users)。queryOrdered ():$($)$($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
完成(true)
}
else {
completion(false)
}
})
}

现在只需在创建新用户时调用这个函数:

$ p $ self_checkUserNameAlreadyExist(newUserName: Johnson){isExist in
isExist {
print(存在用户名)
}
else {
print(创建新用户)



I searched for this question, but none of them had an answer that worked for me.

I want to make it so when a user registers an account, it checks to see if the username they have entered already exists, before creating the account. I have tried using querying in firebase, but I just can't seem to get it to work.

Here is an image of what my firebase data looks like: My firebase data tree

How would I use query to find the string for the key "username"?

解决方案

You can go like this, make one function with completion block to check username is already exist in your Firebase DB and create new user on the basis of it

func checkUserNameAlreadyExist(newUserName: String, completion: @escaping(Bool) -> Void) {

    let ref = FIRDatabase.database().reference()
    ref.child("users").queryOrdered(byChild: "username").queryEqual(toValue: newUserName)
              .observeSingleEvent(of: .value, with: {(snapshot: FIRDataSnapshot) in

        if snapshot.exists() {
            completion(true)
        }
        else {
            completion(false)
        }
    })
}

Now simply call this function when you create new user:

self.checkUserNameAlreadyExist(newUserName: "Johnson") { isExist in
    if isExist {
        print("Username exist")
    }
    else {
        print("create new user")
    }
}

这篇关于Firebase查询唯一的用户名swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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