我正在构建聊天应用程序,有点像whatsapp。我想在创建新组时显示用户设备联系人列表中的已注册应用程序用户列表。现在,为了做到这一点,我必须将每个联系人号码与firebase firestore用户进行比较。而且任何普通用户在设备中都可以有超过500个联系人。此外,firestore对于查询数据库有限制,所以我不能一次比较一个以上的数字,整个过程几乎需要6-7分钟,而且每次读取操作的成本都很低。
我如何克服这种情况,或者处理这种特殊情况的更好方法是什么?
发布于 2018-08-27 18:44:51
您可以将用户的联系人存储在设备上,并且仅将其作为备份发送到firestore。然后,您可以在应用启动时将您的本地数据库与firestore同步。你需要的操作在firebase中是不可能健壮的。即使这样,如果您想在firebase数据中进行搜索,您也需要使用第三方搜索解决方案,如Elastic search和firebase数据,以执行复杂的搜索。
对于本地数据库,您可以使用Room库:https://developer.android.com/topic/libraries/architecture/room
要在Firebase中使用弹性搜索,可以看看这个实用程序:https://github.com/FirebaseExtended/flashlight。
发布于 2018-08-29 22:34:16
OP要求提供一个结构和一些代码(Swift、Firebase数据库)作为解决方案。我将介绍两个选项
如果要使用Firebase查询来查看电话号码是否存在,可能的结构为
users
uid_0
contact_name: "Larry"
contact_phone: "111-222-3333"
uid_1
contact_name: "Joe"
contact_phone: "444-555-6666"然后使用swift代码查询现有号码
let phoneNumbers = ["111-222-3333","444-555-6666"] //an array of numbers to look for
let myQueryRef = self.ref.child("users")
for contactPhone in phoneNumbers {
let queryRef = myQueryRef.queryOrdered(byChild: "contact_phone").queryEqual(toValue: contactPhone)
queryRef.observeSingleEvent(of: .childAdded, with: { snapshot in
if snapshot.exists() {
print("found \(contactPhone)") //or add to array etc
}
})
}通常不推荐在紧凑的循环中使用这样的查询,但它通常适用于迭代次数较少的我。然而,查询比.observers有更多的开销。
IMO,一个更好且更快的选择是只保留一个电话号码的节点。然后遍历您要查找的节点,并使用.observe查看该节点是否存在。
phone_numbers
111-222-3333: true
444-555-6666: true然后执行代码以查看数组中的元素是否存在
let phoneNumbers = ["111-222-3333","444-555-6666"] //an array of numbers to look for
let phoneNumberRef = self.ref.child("phone_numbers")
for contactPhone in phoneNumbers {
let ref = phoneNumberRef.child(contactPhone)
ref.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists() {
print("found \(contactPhone)")
}
})
}在测试中,第二个解决方案必须比第一个解决方案更快。
https://stackoverflow.com/questions/52037240
复制相似问题