Saving save file to DB when user is closing the game

Hey!
I am creating a game for android and ios, and I am using Firebase realtime data to store my save files.
Because the game has many features I didn’t want to make a network request to firebase for each action the user made, only when the user has closed the game I will then save everything in one time.

The problem I noticed is that “OnApplicationPause” will sometimes wont let the function finish, what I mean by that is the data is sometimes being saved normally to firebase but sometimes it will not save anything and when I printed a debug.log in the event I noticed it will not print the last debug.logs, so it means that androiod/ios will kill the process and wont let it finish. This is a huge problem for me and I wanted to know if anyone here found a way to make this work?

(I tried to use OnApplicationQuit but this is not called at all in android)

Thanks for any help!

I am not sure what the time limit is on getting out of dodge, but it’s not intended to be long, and likely varies depending on operating system targeted, and even version of operating system.

Ideally you set your game up to have almost everything you need written to disk and you only need a little bit more written at that close point. One model for this is to periodically stream the full save data out to a new file on disk (you would intelligently choose when to do this during normal application lifetime), and then on exit you actually write out just the name of that file.

The other cool benefit of this is if you get an unexpected crash while writing a savefile, the previous savefile’s name is not overwritten, and the user loses no more than a single session. It would of course be up to you to periodically prune the old no longer accessible savefiles.