[Unity] Realtime database issues on iOS & Android

2,905 views
Skip to first unread message

Jeremy Burgess

unread,
Nov 22, 2016, 3:36:56 PM11/22/16
to Firebase Google Group
Hi all,

Just been trying to get realtime database working in a new app we're building, and I have it all working using editor authentication in the Unity Editor. On iOS and Android however, it's not currently working, and is logging different errors on both. Right now I'm just trying to actually get the device reading and writing before I subscribe a live channel (we have a web service using the REST API to push live data into the database successfully already). On iOS I get the following:

InvalidOperationException: The firebase realtime database has not been properly initialized.

  at Firebase.Database.Core.FirebaseConfigExtensions.CreatePlatform (Firebase.FirebaseApp app) [0x00000] in <filename unknown>:0 

  at Firebase.Database.Internal.Core.Context.GetPlatform () [0x00000] in <filename unknown>:0 

  at Firebase.Database.Internal.Core.Context.EnsureLogger () [0x00000] in <filename unknown>:0 

  at Firebase.Database.Internal.Core.Context.InitServices () [0x00000] in <filename unknown>:0 

  at Firebase.Database.Internal.Core.Context.Freeze () [0x00000] in <filename unknown>:0 

  at Firebase.Database.Internal.Core.RepoManager.CreateLocalRepo (Firebase.Database.Internal.Core.Context ctx, Firebase.Database.Internal.Core.RepoInfo info, Firebase.Database.FirebaseDatabase firebaseDatabase) [0x00000] in <filename unknown>:0 

  at Firebase.Database.FirebaseDatabase.EnsureRepo () [0x00000] in <filename unknown>:0 

  at Firebase.Database.FirebaseDatabase.GoOnline () [0x00000] in <filename unknown>:0 

  at FirebaseDatabaseTester+<Start>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 

  at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0 


Where on Android I get this:

11-22 17:44:38.203: E/Unity(3557): WebSocket:ws_0 - could not get ip address
11-22 17:44:38.203: E/Unity(3557): System.Net.Sockets.SocketException: No such host is known
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):  
11-22 17:44:38.203: E/Unity(3557): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
11-22 17:44:38.203: E/Unity(3557): WebSocket:ws_0 - could not connect
11-22 17:44:38.203: E/Unity(3557): System.Net.Sockets.SocketException: No such host is known
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):  
11-22 17:44:38.203: E/Unity(3557): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
11-22 17:44:38.203: E/Unity(3557): WebSocket:ws_0 - error during handshake
11-22 17:44:38.203: E/Unity(3557): Firebase.Database.Internal.TubeSock.WebSocketException: unknown host: bags-1470031240116.firebaseio.com ---> System.Net.Sockets.SocketException: No such host is known
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   --- End of inner exception stack trace ---
11-22 17:44:38.203: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.CreateSocket () [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.RunReader () [0x00000] in <filename unknown>:0
11-22 17:44:38.203: E/Unity(3557):  
11-22 17:44:38.203: E/Unity(3557): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
11-22 17:44:58.778: E/Unity(3557): WebSocket:ws_1 - could not get ip address
11-22 17:44:58.778: E/Unity(3557): System.Net.Sockets.SocketException: No such host is known
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):  
11-22 17:44:58.778: E/Unity(3557): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
11-22 17:44:58.778: E/Unity(3557): WebSocket:ws_1 - could not connect
11-22 17:44:58.778: E/Unity(3557): System.Net.Sockets.SocketException: No such host is known
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):  
11-22 17:44:58.778: E/Unity(3557): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
11-22 17:44:58.778: E/Unity(3557): WebSocket:ws_1 - error during handshake
11-22 17:44:58.778: E/Unity(3557): Firebase.Database.Internal.TubeSock.WebSocketException: unknown host: bags-1470031240116.firebaseio.com ---> System.Net.Sockets.SocketException: No such host is known
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.GetIpAddress (System.String hostName) [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   --- End of inner exception stack trace ---
11-22 17:44:58.778: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.CreateSocket () [0x00000] in <filename unknown>:0
11-22 17:44:58.778: E/Unity(3557):   at Firebase.Database.Internal.TubeSock.WebSocket.RunReader () [0x00000] in <filename unknown>:0

For the record, I have imported both the .json for Android and the plist for iOS and I can see that the relevant plist has been added to my XCode project, so I *believe* all should be well there (by perhaps not?). My code looks like the following - I'm not clear on whether I need to do any other kind of initialisation on device? Do I need to manually create the Firebase app, or will that 'just happen'? I do have the DB requiring auth (rather than being completely public), but the errors in question don't seem to imply a permissions error. I'm also not clear from the documentation whether I need to do anything with the Firebase user I get from Auth prior to accessing the realtime database.

 using UnityEngine;
 
using System.Collections;
 
using Firebase;
 
using Firebase.Database;
 
#if UNITY_EDITOR
 
using Firebase.Unity.Editor;
 
#endif
 
 
public class FirebaseDatabaseTester : MonoBehaviour
 
{
     
private Firebase.Database.DatabaseReference m_dbReference = null;
     
private Firebase.Auth.FirebaseUser m_user = null;
 
     
private bool m_authed = false;
     
private bool m_passedAuth = false;
     
private string m_clientTestContent = "";
 
     
// Use this for initialization
     
IEnumerator Start()
     
{
     
#if UNITY_EDITOR
         
FirebaseApp.DefaultInstance.SetEditorAuthUserId(System.Guid.NewGuid().ToString());
         
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("<my db>");
         
FirebaseApp.DefaultInstance.SetEditorP12FileName("<my p12>");
         
FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("<my user>");
         
FirebaseApp.DefaultInstance.SetEditorP12Password("<my password>");
     
#endif
 
         
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
         
var authTask = auth.SignInAnonymouslyAsync().ContinueWith(task =>
         
{
             
Debug.LogFormat("Complete {0}, Cancelled {1}, Faulted {2}", task.IsCompleted, task.IsCanceled, task.IsFaulted);
             
if (task.IsCompleted && !task.IsCanceled && !task.IsFaulted)
             
{
                 
// User is now signed in.
                 
Debug.Log(task.Result.GetType());
                 m_user
= task.Result as Firebase.Auth.FirebaseUser;
                 m_authed
= true;
             
}
         
});
         m_passedAuth
= true;
         
yield return new WaitUntil(() => authTask.IsCompleted);
 
         
FirebaseDatabase.DefaultInstance.GoOnline();
         m_dbReference
= FirebaseDatabase.DefaultInstance.RootReference;
         
var readTask = FirebaseDatabase.DefaultInstance.GetReference("ClientTest").GetValueAsync().ContinueWith(task =>
             
{
                 
var result = task.Result;
                 
Debug.Log(result.GetValue(true).ToString());
                 m_clientTestContent
= result.GetValue(true).ToString();
             
});
         
yield return new WaitUntil(() => readTask.IsCompleted);
         
var writeTask = m_dbReference.Child("ClientTest").SetValueAsync(SystemInfo.deviceName);
         
yield return new WaitUntil(() => writeTask.IsCompleted);
     
}
 
     
public void OnGUI()
     
{
         
using (var layout = new GUILayout.VerticalScope())
         
{
             
GUILayout.Label(string.Format("Finished Auth: {0}", m_passedAuth));
             
GUILayout.Label(string.Format("Authorised: {0}", m_authed));
             
GUILayout.Label(m_clientTestContent);
         
}
     
}
 
}

Any assistance is greatly appreciated!

Cheers,
Jeremy

Jeremy Burgess

unread,
Nov 22, 2016, 6:08:54 PM11/22/16
to Firebase Google Group
As a quick addendum - I saw the same issue prior to adding the GoOnline call - I added that as an attempt to get it working. 

Benjamin Wulfe

unread,
Nov 22, 2016, 11:49:57 PM11/22/16
to Firebase Google Group
Firebase developer here.
Are you able to run the quickstart sample for the realtime database successfully on android and iOS?

The error you are seeing suggests that it might have something to do with the project's configuration.  The error "The firebase realtime database has not been properly initialized" means that the internal type "Firebase.Database.Unity.UnityPlatform" was stripped out of the build (possibly due to your byte stripping settings?)  We have a preserve attribute on it, but maybe something in your project is ignoring that and optimizing out required code.
You also cannot use the ".Net 2.0 Subset" and instead must use the full .Net framework to use the database.

I would try:
a) Run the database quickstart. (https://github.com/firebase/quickstart-unity)
b) If that works, change the quickstart (code only) to do something similar to what you need
c) run a diff tool to compare the project settings.  It will likely point to the root cause.

PS you should not need to call GoOnline here.

Jeremy Burgess

unread,
Nov 23, 2016, 11:10:17 AM11/23/16
to Firebase Google Group
Hi,

Yeah, I've made sure I didn't have stripping on and I turned off "Strip Engine Code" for iOS, which should mean all of Mono should be included. I did actually manage to get it working on iOS (I'll try again on Android tomorrow!) In the end the solution was to leave in the SetEditorDatabaseURL call even when not running on editor (which I thought probably shouldn't be necessary?) Irrespective - as soon as I left that call in, everything worked fine on device, so that's really positive. 

I'll try out the quickstart sample tomorrow if I run into further issues!

Thanks,
Jeremy 

Benjamin Wulfe

unread,
Nov 23, 2016, 2:34:42 PM11/23/16
to Firebase Google Group
Ok that tells me that something is wrong reading your plist.  If you call that method, it overrides settings obtained from FirebaseApp.
In particular if you would run the following code:

    Debug.Log( FirebaseApp.DefaultInstance.Options.DatabaseUrl);

I suspect this value is null for you for some reason.
You did go through the getting started steps to drop in both a plist and json file generated from our web console, correct?

Jeremy Burgess

unread,
Nov 23, 2016, 6:45:53 PM11/23/16
to Firebase Google Group
Hey Benjamin,

So the DB Url is not null - it seems to be correct. I've also now tried again on Android and for reasons which are unclear to me (as I don't think I've changed anything, and definitely did have the relevant JSON file in my project), that now works fine.

Going back to iOS I've now tried a bunch of different things, and the only thing which seems to make it work is to Set the Editor Database URL? Again, I should stress that without doing that, the DB URL logs out correctly, but the DB fails to initialise, but with that, everything works fine. Does that call do anything to force the backend to initialise the database connection?

Cheers,
Jeremy

Benjamin Wulfe

unread,
Nov 24, 2016, 12:58:48 PM11/24/16
to Firebase Google Group
That's interesting.  I think what's going on with iOS is that if you do not call that method, the entire Firebase.Database.Unity.dll assembly is being excluded from the build -- and that causes the error.  You can verify this by calling any extension method on App from the Firebase.Unity.Editor namespace (eg you can set the EditorUID which does nothing if you don't set up a p12 file).  

Jeremy Burgess

unread,
Nov 29, 2016, 10:20:06 AM11/29/16
to Firebase Google Group
Hi Benjamin - sorry left this for the weekend. On testing your suggestion, that works exactly as you've suggested. I guess one thing that would be interesting to try would be adding the relevant assemblies to a link.xml file. I'll give that a shot tomorrow.

Nicklas Ruiterman

unread,
Nov 29, 2016, 10:20:06 AM11/29/16
to Firebase Google Group
for me i must call SetEditorDatabaseUrl in the build code

Jeremy Burgess

unread,
Nov 29, 2016, 6:54:26 PM11/29/16
to Firebase Google Group
OK - I've managed to get it going with the link.xml file, so that seems to be the whole problem. I've attached the link.xml I used, which probably has stuff in it which isn't required, but does work (because of build turnaround I was a bit overzealous in what I protected from stripping!)

Note that only protecting the publicly accessible "Firebase.Database" namespace in the Firebase.Database assembly is insufficient to get around the issues, though it does mean it just fails silently (which is odd). I haven't tested but it might be possible to go with a really simple link.xml which just uses the Firebase.Database assembly with preserve=all on it. I've had issues with using link.xml in that way before so I went with the approach I felt was most likely to work in the shortest time.

Finally, going through this process has demonstrated that "Strip Engine Code" is immaterial in getting this to work (which is what I'd expect, as that flag should only effect unneeded Unity code), which is good as being able to leave that on cuts build size.

Inline: 
<linker>
<assembly fullname="Firebase.App">
    <namespace fullname="Firebase" preserve="all"/>
</assembly>
<assembly fullname="Firebase.Auth">
    <namespace fullname="Firebase.Auth" preserve="all"/>
</assembly>
<assembly fullname="Firebase.Database">
    <namespace fullname="Firebase.Database" preserve="all"/>
    <namespace fullname="Firebase.Database.Core" preserve="all"/>
    <namespace fullname="Firebase.Database.DotNet" preserve="all"/>
    <namespace fullname="Firebase.Database.Internal" preserve="all"/>
</assembly>
<assembly fullname="Firebase.Database.Unity">
    <namespace fullname="Firebase.Database" preserve="all"/>
    <namespace fullname="Firebase.Database.Unity" preserve="all"/>
</assembly>
</linker>
  
link.xml

Benjamin Wulfe

unread,
Nov 30, 2016, 2:16:05 PM11/30/16
to Firebase Google Group
Thanks for the extra research and posting back here Jeremy.  We'll try to get the fix into a release in the near future and report back.
And thanks for your patience.

-Ben

Stewart Miles

unread,
Dec 9, 2016, 4:19:45 PM12/9/16
to Firebase Google Group
I'm pleased to announce that we released:
  • Firebase C++ SDK 2.1.0 (release notes here, download here)
  • Firebase Unity SDK 1.1.0 (release notes here, download here)
We added a link.xml to the SDK which should resolve this for everyone.

Cheers,
Stewart
Reply all
Reply to author
Forward
0 new messages