I'm writing a small game for Android in Unity. Basically the person have to guess whats on the photo. Now my boss wants me to add an additional function-> after successful/unsuccessful guess the player will get the panel to rate the photo (basically like or dislike), because we want to track which photos are not good/remove the photos after a couple of successful guesses. My understanding is that if we want to add +1 to the variable in Firebase first I have to make the call and get it then we have to make a separate call with adding 1 to the value we got. I was wandering if there is a more efficient way to do it? Thanks for any suggestions!
3 Answers
Instead of requesting firebase when you want to add ,you can request firebase in the beginning (onCreate like method) and save the object and then use it when you want to update it. thanks

- 81
- 2
- 3
-
Thank you for the suggestion, I will think about it! :) – Piotr Sep 06 '19 at 10:10
Well, one thing you can do is to store your data temporarily in some object, but NOT send it to Firebase right away. Instead, you can send the data to Firebase in times when the app/game is about to get paused/minimized; hence, reducing potential lags and increasing player satisfaction. OnApplicationPause(bool)
is one of such functions that gets called when the game is minimized.

- 1,244
- 1
- 10
- 23
-
Thank you, didn't think about it. Sounds like a very good suggestion! :) – Piotr Sep 06 '19 at 10:10
-
@Piotr Glad. Make sure to upvote decent answers and select one as the best answer if it actually was the best answer. Welcome to SO! – Sean Goudarzi Sep 07 '19 at 08:00
To do what you want, I would recommend using a Transaction instead of just doing a SetValueAsync. This lets you change values in your large shared database atomically, by first running your transaction against the local cache and later against the server data if it differs (see this question/answer).
This gets into some larger interesting bits of the Firebase Unity plugin. Reads/writes will run against your local cache, so you can do things like attach a listener to the "likes" node of a picture. As your cache syncs online and your transaction runs, this callback will be asynchronously triggered letting you keep the value up to date without worrying about syncing during app launch/shutdown/doing your own caching logic. This also means that generally, you don't have to worry too much about your online/offline state throughout your game.

- 2,993
- 1
- 13
- 11