gpt4 book ai didi

firebase - 在 Firestore 中创建关注者和关注模型的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-04 17:30:47 26 4
gpt4 key购买 nike

我试图制作这样的追随者和追随者模型。所以每个用户都会有关注者和关注者子集

enter image description here

我只保存文档中以下子集合中的最小数据,例如个人资料图片路径、uid 和全名

enter image description here

但是这个模型的问题是,当用户在“用户”父集合中更改他们的个人资料图片或显示名称时,随后关注者的以下子集合中的姓名和个人资料图片不会更新。

所以我想我需要使用可调用的云函数来解决这个问题以更新以下子集合中的所有文档

exports.callableUpdateUserDataInOtherDocuments = functions.https.onCall(async (data, context) => {



try {

const verifiedUserSnapshot = await db.doc(`users/${context.auth.uid}`).get()

const userData = verifiedUserSnapshot.data()


// update display name in following subcollection of the followers

const changeUserDataInFollowersPromises = []
const followersSnapshot = await db.collection(`users/${context.auth.uid}/followers`).get()

followersSnapshot.forEach( doc => {

const followerID = doc.data().uid
const p = db.doc(`users/${followerID}/following/${userData.uid}`).update({
fullname: userData.fullname,
profilePicturePath: userData.profilePicturePath
})

changeUserDataInFollowersPromises.push(p)
})

return Promise.all(changeUserDataInFollowersPromises)

} catch(error) {
console.log(error)
return null
}





})

但是使用此云功能更新对我来说似乎效率不高,如果用户有 10.000 个关注者,那么我需要阅读 10.000 个文档并更新 10.000 个文档。而且似乎控件在用户中,因为如果他们在 1 分钟内更新数据 5 次,那么我需要阅读 50.000 个文档并更新 50.000 个文档。

我还需要更新显示名称和个人资料图片路径,因为以下子集合中的文档将显示在回收站 View (Android)和表 View (iOS)中

我不知道,我是 Firestore 和 NoSQL 数据库的新手,这是一种常见的方法还是有更好的方法来解决这个问题?

最佳答案

确实,对于您的情况,如果用户有很多关注者,您将需要执行大量操作。您需要平衡您对系统的思考方式,因为 Firestore 和 Cloud Functions 将能够处理和扩展性能,因此您不会遇到任何与之相关的问题。
但是,计费可能是您将面临的主要问题。对于您的用例,它必须在这段时间内更新数据库。
我进行了搜索,发现以下讨论链接可能会帮助您决定您的系统及其工作方式。

  • Firestore - how to structure a feed and follow system
  • What is the efficient way to model follower/following/mutual friends using Firestore?
  • 关于firebase - 在 Firestore 中创建关注者和关注模型的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59958468/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com