swift - 检查用户名是否存在于 Firebase
问题描述
我有一个这样的数据库
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 并检查用户名是否存在
谢谢你的帮助 !
解决方案
在安卓中
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: 存在
Desc :如果 DataSnapshot 包含非空值,则返回 YES。
对于斯威夫特:
存在()
函数存在()->布尔
推荐阅读
- python - 如何在 scikit-multiflow 评估之前标准化数据?
- sql - 改进 Excel VBA 中的 SQL 查询
- javascript - 如何遍历嵌套对象并将值放入数组?
- mysql - 如何过滤 3 个相关的 datagridviews
- typescript - Typescript 将对象键推断为数字而不是字符串
- xml - XML:感叹号是什么意思
- java - 如何修复错误“状态机未处于准备执行删除的状态”
- bash - 从索引数组中提取索引:保证是有序的?
- spring-boot - 开发通用微服务的最佳实践
- shell - 如何使用 MMD Parser 生成带有页码的目录