Jared
Jared

Reputation: 2217

Firebase Firestore Android Deleting Multiplayer Game Documents

So I have a game where two devices/users are connected to a firestore Game document. At the end of the game, I load the game results to both users and then they move on to another game. My issue right now is thinking of the best way of deleting completed game objects. Once a game is completed it no longer needs to be in firestore. I just tested putting gameDocRef.delete() for both users at the end but then occasionally one player deletes it too fast and the other can't load the results.

Is there a better/automated way of cleaning up completed documents? Maybe Firebase Functions or something? Any advice is apprectiated :)

Upvotes: 0

Views: 142

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76769

you could store two values; eg. "player one acknowledged" & "player two acknowledged",

in order to confirm, when both of the players had fetched the results of the game -

and delete only those documents, where both of them had acknowledged

(possibly even with a Cloud Functions cleanup script, instead of the client).

a) when looking at the events available, the update trigger would appear the most suitable:

providers/cloud.firestore/eventTypes/document.update

with the downside, that it would be triggered quite often, because it is per document and not per field.

b) alternatively, one could batch the deletions, triggering via HTTP, with a cronjob on another host .

here would be an example, using App Engine Cron (as alternative to Linux crontab on GCE).

Upvotes: 2

Related Questions