问题
I have coded a match making system and this system works like that --> when a create operation came to firestore it matches with an older record and the cloud function deletes both of them. But i wonder that when 2 new records are created, is there any chance for matching these 2 with the same old record because the deletion process takes some time?
Also, if i do that process with update, is it faster than delete?
EDIT:
Here is my matchMaker Cloud Function:
exports.matchMaker = functions.firestore
.document("WaitingRooms/{gameId}/Users/{matchId}")
.onCreate(async (snap, context) => {
const data = snap.data();
const gameId = context.params.gameId;
const data2 = await db.collection("Users").doc(data["userId"]).get();
const data3 = await db
.collection("WaitingRooms")
.doc(gameId)
.collection("Users")
.where("userId", "!=", data["userId"])
.limit(1)
.get();
if(data3.docs[0])
{
let match = data3.docs[0].data()["userId"]
matchPN = await db.collection("Users").doc(match).get();
matchPushNotifications = matchPN.data()["pushNotificationsTokens"];
myPushNotifications = data2.data()["pushNotificationsTokens"];
await db
.collection("Users")
.doc(match)
.update({
waitingMatches: admin.firestore.FieldValue.arrayUnion({id:data["userId"]}),
});
await db
.collection("Users")
.doc(data["userId"])
.update({
waitingMatches: admin.firestore.FieldValue.arrayUnion({id:match}),
});
//SOME OTHER CODES
await db
.collection("WaitingRooms")
.doc(gameId)
.collection("Users")
.doc(data3.docs[0].id)
.delete();
await db
.collection("WaitingRooms")
.doc(gameId)
.collection("Users")
.doc(context.params.matchId)
.delete();
}
});
This code work correctly but the problem is that if Waiting Rooms/{gameId}/Users/
has three records, the code matches 3 of them but i want to match 2 of them and one of them has to remain.
I know cloud function can not know which one is matched or not but how can i check that?
来源:https://stackoverflow.com/questions/66185686/firestore-delete-vs-update-time-and-performance