What I want to do
I have a small game with Unity, where I store a user score in firebase realtime database to display leaderboard later, when the user enters the game the first time, I also ask username and save it with deviceId(to identify the device). So now I want to handle the situation when the user don't have connection to internet, I want to store that data in the device and when user gains connection or enters the game again I will update data in firebase realtime database.
What is my problem
The problem here is that firebase doesn't throw any exception that there is no connection, and it just keeps trying to connect to internet until there is connection. You may suggest to check internet connection when user enters the game, but I want to handle it also in situations where the user for example will start with internet connection and lets say during the game the user will lose internet, if that happened I want to store data in the device as I mentioned above. So I need to catch the situation of no internet connection to be able to store data in the device.
What I have tried
1)
DatabaseReference connectedRef = FirebaseDatabase.DefaultInstance.GetReference(".info/connected");
connectedRef.ValueChanged += (object sender, ValueChangedEventArgs a) => {
bool isConnected = (bool)a.Snapshot.Value;
Debug.Log(isConnected);
};
doesn't fire when user loses internet connection during the game, so I'm unable to catch no internet connection exception
2)
Task timeoutTask = Task.Delay(1000);
Task firebaseConnection = await database.Child("users").GetValueAsync();
if (await Task.WhenAny(firebaseConnection, timeoutTask) == firebaseConnection) {
Debug.Log("timeout");
} else {
Debug.Log("completed");
}
I thought that putting timeout on firebase connection would be the only solution at the moment, but this doesn't work too Debug.Log("timeout"); never logs when there is no internet connection.
I've been searching a solution for this and found only those 2 answer which seemed possible to work, but no luck, I'm out of ideas and don't know how to solve this issue. Looking forward for your help, thanks.