【问题标题】:check if the username exist in Firebase [duplicate]检查用户名是否存在于 Firebase [重复]
【发布时间】:2018-11-27 02:34:24
【问题描述】:

我有一个这样的数据库

users/UID1/“用户名”(用户 1)

 /UID2/"Username" (of user 2)
 /UID3/"Username" (of user 3)
 /UID4/"Username" (of user 4)

等等..

我想检查用户名是否存在,但我无法使用所有现有的 UID 进入循环。 目前我已经尝试过:

let databaseRef = Database.database().reference()        
            databaseRef.child("users").child("uid").child("Username").observeSingleEvent(of: DataEventType.value, with: { (snapshot) in
                switch snapshot.value {
                case let value as Bool where value:
                    // value was a Bool and equal to true
                    print ("username found")
                default:
                    // value was either null, false or could not be cast
                    print ("username not found")
                }
            })

        }

我不知道该放什么而不是 child("uid") 循环到我数据库中的每个 uid 并检查用户名是否存在

感谢您的帮助!

【问题讨论】:

  • 你好,有没有办法循环每一个UID?
  • 请忘记我之前的评论,我的咖啡还没开始。这是可能的,但更改数据结构会更容易。您可以查看Daniel's answer
  • 检查我链接的问题。具体检查我自己的答案,因为接受的答案加载的数据比必要的多:stackoverflow.com/a/50494190

标签: swift firebase firebase-realtime-database


【解决方案1】:

实现这一点的最简单方法是在注册用户时,使用其唯一的 UID 创建用户,并将他们的所有数据保存在其中,就像你正在做的那样,但还要创建一个名为“用户名”的节点,它只包含所有用户名使用密钥作为他们的用户名和值作为 1 进行注册,如下所示:

Usernames {
 - username: 1
}

当用户注册然后去输入用户名时,你可以像这样检查它是否存在:

let username = "username that user has typed in"

let reference = Database.database().reference()
reference.child("usernames").observeSingleEvent(of: .value, with: { (snapshot) in

    if snapshot.hasChild(username) {

        print("Already exists")

    } else {

        print("Doesn't exist")

    }

}, withCancel: nil)

编辑:

感谢@FrankvanPuffelen,这是一种更有效的方法 - 无需遍历每个用户名。

let reference = Database.database().reference()
reference.child("usernames").child(username).observeSingleEvent(of: .value, with: { (snapshot) in

    if snapshot.exists() {

        print("Username already exists")

    } else {

        print("Username doesn't already exist")

    }

}, withCancel: nil)

【讨论】:

  • 除了使用 1 作为值,您还可以使用 uid 将用户名链接到用户。
  • @AndréKool 好点。很有用
  • 谢谢两位!这是一个很好的观点
  • 如果您需要检查是否存在,请不要加载所有用户名。这是对带宽的浪费。取而代之的是reference.child("usernames").child(username).observeSingleEvent(of: .value, with: { (snapshot) in if snapshot.exists() {,正如我在这里展示的那样:stackoverflow.com/a/50494190/209103
  • 谢谢丹尼尔。感谢您帮助开发人员了解查找图。对于刚接触 Firebase/NoSQL 的开发人员来说,这些总是令人困惑,因为大多数关系数据库都内置了这些东西。
【解决方案2】:

在 Android 中

DataSnapshot 实例包含来自 Firebase 数据库位置的数据。每当您读取数据库数据时,您都会以 DataSnapshot 的形式收到数据。

DataSnapshot 有exist() 方法检查记录是否存在。基于此文档https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html#exists()

点赞:

reciverDatabase.getRef().addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (dataSnapshot.exists()) {
                                reciverDatabase.child(Constants.CONVERSATION_UNREAD_COUNT).setValue(dataSnapshot.getValue(Long.class) + 1);
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            Crashlytics.logException(databaseError.toException());
                            sendMessageListener.onSendMessageFailure(databaseError.getMessage());
                        }
                    });

在 iOS 中:

你可以像这样检查它:

对于目标 C: 存在

描述:如果 DataSnapshot 包含非空值,则返回 YES。

您可以从该文档中阅读更多内容https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDataSnapshot#/c:objc(cs)FIRDataSnapshot(im)exists

对于斯威夫特:

存在()

func exists() -> 布尔值

https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DataSnapshot#/c:objc(cs)FIRDataSnapshot(im)exists

【讨论】:

  • 你好,这是安卓的吗?我没有得到你的答案
  • @Bonilla 您可以查看我是否提供了链接。 Firebase 具有相同的 DataSnapshot 类来检查记录是否存在。
  • @PeterHaddad 抱歉。但是你现在会检查我是否更新了我的答案。
  • @Bonilla 您现在可以查看。我已经更新了我的答案。
猜你喜欢
  • 2017-02-17
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 2016-07-26
  • 2012-12-12
  • 1970-01-01
相关资源
最近更新 更多