如果 Headers 措辞不当,我会道歉 . 我正在寻求有关在Firebase上执行以下操作的推荐方法的建议 .
我正在使用Firebase作为群组协作类型的应用程序(想想Whatsapp) . 用户使用他的电话号码进行注册,并以用户身份添加到Firebase数据库 . 用户在Firebase上按如下方式存储
users
-KGvMIPwul2dUYABCDEF
countryCode: 1
id: -KGvMIPwul2dUYABCDEF
mobileNumber: 1231231234
name: Varun Gupta
每当用户打开应用程序时,我想检查用户手机联系人列表中的所有用户是否也在使用我的应用程序并在应用程序中显示这些联系人 . 电话号码用于检查手机通讯录中的人是否正在使用该应用程序 . 为实现此目的,我将用户联系人列表存储到Firebase,触发Firebase功能以使用我的应用计算联系人并将其单独存储在Firebase上 .
为了确定谁在使用我的应用,我创建了一个Firebase中所有用户的 Map ,其中包含电话号码以及国家/地区代码和电话号码的组合 . 该值是用户ID,对于上面的示例,该用户ID为 -KGvMIPwul2dUYABCDEF
. 因此, Map 将为用户提供以下两个条目
{
1231231234: -KGvMIPwul2dUYABCDEF
11231231234: -KGvMIPwul2dUYABCDEF
}
我为所有用户创建了上面的内容,然后我只查询每个联系人,如果 Map 中有用户电话号码的条目,并找出使用该应用程序的用户列表 .
以下是代码的摘录 . 现在它是在 firebase-queue
工作者中完成的,但我打算将其移动到Firebase功能
// This piece of code is used to read the users in Firebase and create a map as described above
ref.child('users').on('child_added', (snapshot) => {
var uid = snapshot.key;
var userData = snapshot.val();
// Match against both mobileNumber and the combination of countryCode and mobileNumber
// Sanity check
if(userData.mobileNumber && userData.countryCode) {
contactsMap.set(sanitizePhoneNumber(userData.mobileNumber), uid);
contactsMap.set(sanitizePhoneNumber(userData.countryCode + userData.mobileNumber), uid);
}
});
// This piece of code is used to figure out which contacts are using the app
contactsData.forEach((contact) => {
contact.phoneNumbers.forEach((phoneNumber) => {
var contactsMapEntry = contactsMap.get(sanitizePhoneNumber(phoneNumber))
// Don't add user himself to the contacts if he is present in the contacts
if(contactsMapEntry && contactsMapEntry !== uid && !contactsObj[contactsMapEntry]) {
const contactObj = {
name: createContactName(contact),
mobileNumber: phoneNumber.number,
id: contactsMapEntry
}
contactsObj[contactsMapEntry] = contactObj
currentContacts.push(contactObj)
}
});
});
// After figuring out the currentContacts, I do some processing and they are pushed to Firebase which are then synched with the app
我担心的是,随着用户数量的增加,这将开始变慢,因为我正在阅读Firebase中的所有用户在内存中创建此 Map ,以便找出使用该应用程序的联系人的每个请求,或者我会好吗用这种蛮力的方法,不要太担心 .
我是否应该考虑复制下面的数据
contacts
1231231234: -KGvMIPwul2dUYABCDEF
11231231234: -KGvMIPwul2dUYABCDEF
然后只查询 /contacts/{contact phone number}
如果有更好的方法来实现此工作流程,请建议 .