Hi! I’m using unity for mobile devices.
In game I have some data which is saving when the game in entered in background (OnApplicationPause). Earlier for this task I used the file system and all works fine.
But now start using the cloud save and encountered a problem of data saving for our case. When the application pause the unity manages to process one frame. But Cloud save has a code which is saving data in different frames (in case when data keys count in greater than 20). This code is attached below.
In our case I save 56 data keys, but only first 20 completely saves. after this the application stops.
Our code for using Unity Cloud Save:
private Dictionary<string, object> _saveData = new();
public async void SaveFinish()
{
if (_isValid)
{
try
{
await CloudSaveService.Instance.Data.Player.SaveAsync(_saveData);
}
catch (Exception _)
{
}
}
}
How we can resolve this problem? maybe we can setup this count in Cloud save code?
@IainUnity3D Can you help?
1 Like
New: I tried insert all data in one key to avoid the Count check. it didn’t help. Maybe the problem happens because the application entered in background.
The alternative solution - saving data every N seconds, but it is will create a lot of write requests. The best way - save data in OnApplicationPause but it’s not works well.
Hi there,
We’ve run into this being challenging in the past, specifically on mobile where Android and iOS stop running async tasks (like sending data over the network) when an app goes into the background.
There isn’t a quick and easy way around it exactly, it turns out to be a bit tricky and only guaranteed solution to save to the cloud when this happens is to write native code to handle background tasks on each platform then call that code when the app is suspended, which is a bunch of work - especially if you are launching on more than one mobile platform.
The most practical approach is probably to save the data locally and then to sync back to the cloud when the app is resumed, that way it would still be persisted even if the app was quit by the user, or if the phone is restarted.
We are looking to solve for this in a better way in the future, at least eliminating the boiler plate code needed to do something like save locally and sync later, maybe initially just providing an example for developers of how to do this alongside the SDK.
I don’t have an ETA on that but I am interested in hearing when people run into this issue to help us understand the impact.
2 Likes
I was afraid to spell it out on a hunch. It’s good to know that this is still what it takes.
1 Like
Thanks for the answer!
Then we will do like you advise.
Where can I follow the improvements and solutions to this issue?