I have tried to make following and followers model like this. so every users will have following and followers subcollection
I only save minimum data in the document in following subcollection such as profile picture path, uid, and fullname
but the problem in this model is, when the user change their profile picture or their display name in 'users' parent collection, then the name and profile picture in the following subcollection of the follower is not updated.
so I think I need to use callable cloud function to solve this to update all documents in following subcollection
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
}
})
but using this cloud function update seems not efficient to me, if a user have 10.000 followers then I need to read 10.000 documents and update 10.000 documents. and also it seems the control is in the user because if they update the data 5 times in 1 minute then I need to read 50.000 documents and update 50.000 documents.
I need to also update the display name and profile picture path, because the documents in following subcollection will be displayed in recycler view (Android) and Table View (iOS)
I don't know, I am new in Firestore and in NoSQL database, it is a common way or is there a better way to solve this ?