Eray Tuncer
Eray Tuncer

Reputation: 725

Unity Firebase realtime database internal exception

I use firebase realtime database to save my game data. When there is no internet and I try to send the JSON data, it gives me internal errors, frequently. It seems like it tries endlessly to send the data but I only called it once. This causes error log garbage inside the console.

    FirebaseDatabase.DefaultInstance.RootReference
        .Child("users")
        .Child(GetUserID())
        .SetRawJsonValueAsync(json)
        .ContinueWith(task => {
            if (task.Exception != null || task.IsCanceled || task.IsFaulted) {
                Debug.LogError("FirebaseStorageService - Upload User Data - FAILED!");
            } else {
                Debug.Log("FirebaseStorageService - Upload User Data - DONE");
            }
        });

I expect to get inside the callback method passed to ContinueWith and be able to handle the exception there. It seems like 'task.Exception' what I should have. I cannot come to that block either.

The error message I get repeatedly:

09-13 11:50:51.777 30555 31681 E Unity   : 09/13/2018 08:50:51 [Error] WebSocket: ws_5 - WebSocketException during handshake
09-13 11:50:51.777 30555 31681 E Unity   : Firebase.Database.Internal.TubeSock.WebSocketException: unknown host: cooking-game-88ssd.firebaseio.com ---> System.Net.Sockets.SocketException: No such host is known
09-13 11:50:51.777 30555 31681 E Unity   :   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
09-13 11:50:51.777 30555 31681 E Unity   :   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
09-13 11:50:51.777 30555 31681 E Unity   :   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
09-13 11:50:51.777 30555 31681 E Unity   :   --- End of inner exception stack trace ---
09-13 11:50:51.777 30555 31681 E Unity   :   at Firebase.Database.Internal.TubeSock.WebSocket.CreateSocket () [0x00000] in <filename unknown>:0
09-13 11:50:51.777 30555 31681 E Unity   :   at Firebase.Database.Internal.TubeSock.WebSocket.RunReader () [0x00000] in <filename unknown>:0

Update 1: I have just figured out that it is not just about sending or retrieving. I got those message even with the statement below:

DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;

Update 2:

Firebase Version 4.2

Upvotes: 1

Views: 1846

Answers (2)

Dr-Bracket
Dr-Bracket

Reputation: 5494

It could also be due to invalid characters in your path (.Child) name.

You can test invalid characters by adding data manually. If you see "Path contains invalid characters", it will throw internal exception.

enter image description here


In my case, I had to change a string like House-content-324xn4.txt to simply House-content-324xn4.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

This is working as intended, although clearly not as you expected. :-)

The task only completes if:

  • Either the write is committed to disk on the server.
  • Or the write is rejected by the (security rules on the) server.

The Firebase SDK doesn't treat a missing connection as a fatal error. It keeps a queue of pending write operations, and retries them until they succeed/fail on the server. Only then will the task complete.

This is usually great when you have a connection that occasionally drops (i.e. on mobile while moving), since Firebase handles the intermittent connectivity loss automatically.

If you want your code to not even try to write when there's no connection, you'll want to either use the internet detection API of your platform, or listen to Firebase's .info/connected path to ensure you are connected to the Firebase back-end before trying to write. You'd typically put the latter in a global state, that you then check before any write to the database.

Upvotes: 1

Related Questions