firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
825 stars 428 forks source link

Use realtime database Emulator from Unity #1218

Open DiscoverTravel opened 2 years ago

DiscoverTravel commented 2 years ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

Unity crash if use local instance of Database (Emulator) In unity crash log: ERROR: Could not initialize persistence: Unable to find app data directory. Crash !!!

Steps to reproduce:

Use this code to connect to the emulator

Relevant Code:

FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(async task =>
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == DependencyStatus.Available)
            {
                FirebaseDatabase db = Firebase.Database.FirebaseDatabase.GetInstance("http://localhost:9000/?ns=myns");
                var snapshot = await db.GetReference("test").GetValueAsync();
                Debug.Log("The value: " + snapshot.Value);
            }
        });
paulinon commented 2 years ago

Hi @DiscoverTravel,

Could you provide the complete and detailed steps to replicate this behavior? I haven't encountered the issue using the information you provided so far.

DiscoverTravel commented 2 years ago

Hi @paulinon, Thanks for the reply.

The steps are simple:

paulinon commented 2 years ago

Thanks for the additional information, @DiscoverTravel. Could you provide the full script containing the relevant code? I still haven't encountered the error message, and I think I may be missing something in my implementation.

DiscoverTravel commented 2 years ago

Hi @paulinon, this is the code. Remember to attach it as a script component to a GameObject of the project.

Best regards

using Firebase;
using Firebase.Database;
using Firebase.Extensions;
using UnityEngine;

public class FirebaseManager : MonoBehaviour
{

    void Start()
    {
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
        {

            var dependencyStatus = task.Result;
            if (dependencyStatus == DependencyStatus.Available)
            {

                Debug.Log("Firebase is ok");
                var app = FirebaseApp.DefaultInstance;

                FirebaseDatabase db = Firebase.Database.FirebaseDatabase.GetInstance("http://localhost:9000/?ns=myns");
                db.GetReference("test").GetValueAsync().ContinueWithOnMainThread(task =>
                   {
                       if (task.IsFaulted)
                       {
                           Debug.Log("Error");
                       }
                       else if (task.IsCompleted)
                       {
                           DataSnapshot snapshot = task.Result;
                           Debug.Log(snapshot.Value);
                       }
                   });
            }
            else
            {
                UnityEngine.Debug.LogError(System.String.Format("Could not resolve all Firebase dependencies: {0}", dependencyStatus));
            }
        });
    }

}
paulinon commented 2 years ago

Thanks for that, @DiscoverTravel. I wasn't able to replicate the issue using the steps you provided, but I did encounter the error message whenever Force Resolve returned an error.

Could you confirm if there are issues when going to Assets > External Dependency Manager > Android Resolver > Force Resolve? If there aren't any, could you replay the scene and see if the issue persists?

google-oss-bot commented 2 years ago

Hey @DiscoverTravel. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

DiscoverTravel commented 2 years ago

Hello, for me the problem still exists. Force resolve succeeds. There are other threads on the web about this issue. I don't know what to add ... Windows 10

Greetings

paulinon commented 2 years ago

Hi @DiscoverTravel,

It's possible that you're facing a setup-related issue. Also, I got word from the team that this isn't officially supported yet as the Firebase Local Emulator Suite is currently in Beta. That being said, I'll mark this as a feature request for now.

You may refer to this thread for any updates.

mrchantey commented 2 years ago

Plus 1 for this feature please! It would greatly reduce the friction in our automated testing workflow.