Firebase - 检查高效的用户名 [英] Firebase - Check efficiently username taken

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

问题描述

我开发了一个小函数来检查用户输入的用户名是否可用。尽管如此,该功能使用迭代和条件在本地在设备上进行检查。我想这可能会成为未来的性能瓶颈,当那个列表中有成千上万的用户时(每次成功创建一个新用户时都会更新列表)。有没有办法在后端更有效地做到这一点?



这是现在可以使用的功能:

  func checkUsernameTaken(completion:(result:NSError) - > Void)
{
let errorFound:NSError = NSError(domain:,code: 0,userInfo:nil)

dbReference.child(usernamesTaken)。observeEventType(.Value,withBlock:{(snapshot:FIRDataSnapshot!) - & = nil)
{
for snapshot.children.allObjects as rest![FIRDataSnapshot] {
if(rest.value as?String == self.usernameTxtField.text!){
self.isUsernameTaken = true
} else {
self.isUsernameTaken = false
}
}
completion(result:errorFound)
} else {
self.isUsernameTaken = false
完成(result:errorFound)
}
})
}

Firebase中的数据结构是这样的:



这个列表会像前面说的那样增长,但现在只是测试的目的。



在此先感谢

更新:

  func usernameValidation(完成:(result:NSError) - >无效)
{
let errorFound:NSError = NSError(domain: ,code:0,userInfo:nil)

dbReference.child(usernamesTaken)。queryEqualToValue(self.usernameTxtField.text!)。observeEventType(.Value,withBlock:{(snapshot:FIRDataSnapshot! ) - >无效

print(snapshot.childrenCount)
})
}


解决方案

不要自己做搜索,让Firebase执行查询。 FIRDatabaseQuery 允许您通过键或值来排序和过滤孩子。在你的情况下,你可以使用 queryEqualToValue 来找到匹配 usernameTxtField.text的 usernamesTaken / code>。


I developed a small function that checks if the username entered by registering guest is available or not. Nonetheless, the function checks this locally on the device using the iteration and if condition. I am thinking this may become a performance bottleneck in future when there are thousands of users in that list (the list is updated every time a new user is created successfully). Is there a way I can do this more efficiently on the backend side?

Here is the function that works now:

func checkUsernameTaken(completion: (result: NSError) -> Void)
    {
        let errorFound:NSError = NSError(domain: "", code: 0, userInfo: nil)

        dbReference.child("usernamesTaken").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot!) -> Void in
            if(snapshot != nil )
            {
                for rest in snapshot.children.allObjects as! [FIRDataSnapshot]  {
                    if(rest.value as? String == self.usernameTxtField.text!){
                        self.isUsernameTaken = true
                    }else{
                        self.isUsernameTaken = false
                    }
                }
                completion(result:errorFound)
            }else{
                self.isUsernameTaken = false
                completion(result:errorFound)
            }
        })
    }

The data structure in Firebase is this:

This list will grow as said earlier but for now it is short for testing purposes only.

Thanks in advance

Update:

 func usernameValidation(completion: (result: NSError) -> Void)
    {
        let errorFound:NSError = NSError(domain: "", code: 0, userInfo: nil)

        dbReference.child("usernamesTaken").queryEqualToValue(self.usernameTxtField.text!).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot!) -> Void in

            print(snapshot.childrenCount)
        })
    }

解决方案

Instead of doing the search yourself, let Firebase execute a query. FIRDatabaseQuery allows you to order and filter children by key or value. In your case you can use queryEqualToValue to find children of usernamesTaken that match usernameTxtField.text.

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

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