firebase / firebase-unity-sdk

The Firebase SDK for Unity
http://firebase.google.com
Apache License 2.0
216 stars 35 forks source link

FirebaseApp.Create not override default instance #379

Open Thaina opened 4 years ago

Thaina commented 4 years ago

Please fill in the following fields:

Unity editor version: 2018.421f Firebase Unity SDK version: 6.12.0 Source you installed the SDK : unitypackage Firebase plugins in use : Auth FireStore Additional SDKs you are using : Facebook AdMob VoxelBusters Platform you are using the Unity editor on : Windows Platform you are targeting : Android Scripting Runtime : IL2CPP

Please describe the issue here:

(Please list the full steps to reproduce the issue. Include device logs, Unity logs, and stack traces if available.)

    FirebaseApp.LogLevel = Firebase.LogLevel.Verbose;
    var status  = await FirebaseApp.CheckAndFixDependenciesAsync();
    var main = FirebaseApp.Create(new AppOptions() {
        ProjectId = "{another}",
        StorageBucket = "{anotherBucket}"
    });

With the code above, the main.Options.ProjectId and DefaultInstance still being the ID from config.json. Is it a bug or is it intended?

Please answer the following, if applicable:

Have you been able to reproduce this issue with just the Firebase Unity quickstarts (this GitHub project)?

What's the issue repro rate? (eg 100%, 1/5 etc)

patm1987 commented 4 years ago

The FirebaseApp.Create issue is expected behaviour. There is limited support in Firebase for multiple projects in a single application.

The main.Options.ProjectId being incorrect sounds like a bug. Can you elaborate a little more on what exactly you're seeing?

Ex: do you mean that you're literally typing:

var main = FirebaseApp.Create(new AppOptions() {
    ProjectId = "{another}",
    StorageBucket = "{anotherBucket}"
});

then main.Options.ProjectId is the id from the JSON. Or are you checking FirebaseApp.DefaultInstance.Options.Projectid?

If you can confirm the main.Options one being incorrect in one of the quickstarts, I can get it into our but list.

Thaina commented 4 years ago

@patm1987 I don't have access to the project on weekend but if I not make any mistake, The option of the FirebaseApp I get from Create without specify a name, or even specify the DefaultName as a name on Create is exactly the same as DefaultInstance. Its like Create was check the dictionary and just return the instance of that name in dictionary

Yes, yesterday I was debugging and seem like main.Options.ProjectId is the id from the JSON

Thaina commented 4 years ago

@patm1987 Now I can confirm that this code

var overridden = FirebaseApp.Create(new AppOptions() {
    ProjectId = another,
    ApiKey = apikey,
    AppId = appid,
});

Debug.LogFormat("overridden.Options.ProjectId : {0}",overridden.Options.ProjectId);

Will still print the id in config json, not the another key I put there, at least in editor, don't know this behaviour also the same in actual device

I suspect that FirebaseApp.Create will always return app that was already created with the specified name. And will not override the app that was already created by any new AppOptions. So the DefaultApp was always created first won't get replaced and will not returned

DellaBitta commented 4 years ago

Hi all,

Thanks for following up.

I was able to successfully override the ProjectId in a secondary app I created. Could you wrap the attempt to create the new app in a try / catch block to see if an exception is being thrown? Thanks!

Thaina commented 4 years ago

@DellaBitta There was no exception being thrown, it just return app instance normally but the AppID was not the same one I was specified

DellaBitta commented 4 years ago

Ok, I've opened an internal bug to track this issue. Thank you!

PedroMR commented 4 years ago

I seem to have a similar issue. For one of our projects the creation fails and falls back to the default instance.

chkuang-g commented 3 years ago

Hi folks,

I think you at least need to setup ApiKey, AppId and ProjectId. Unfortunately those fields does not default to the google-services.json or GoogleServices-Info.plist in your project.

https://firebase.google.com/docs/reference/unity/class/firebase/app-options

Can you confirm again and see if you still cannot create FirebaseApp that way?

Thaina commented 3 years ago

@chkuang-g I still get the same result as I try to report

The point is, this is not only about first creation, its about multiple creation. The FirebaseApp.Create function cache the precede creation by name without creating new app if the name is not changed. Which I think it should not be expected for Create function

Here is how to reproduce. You need to have 2 project and let one be default from google-services.json and another one specified directly by AppOptions

var main0 = FirebaseApp.Create();

var main1 = FirebaseApp.Create(new AppOptions() {
    ProjectId = specificName,
    ApiKey = specificKey,
    AppId = specificID
});

Assert.That(main1.Options.ProjectId,Is.EqualTo(specificName));

The assert will be failed. The name would be from the first creation, instead of overriding the existing app

cynthiajoan commented 3 years ago

Hi @Thaina, I checked the implementation detail, and yes if we see the project name is same with the default name, we will use the default option that set in the json files. I'd like to get more context of the use case that we have the need of creating multiple projects with the same default name but different options, based on that we can discuss if we want to change this behavior.

Thanks!

Thaina commented 3 years ago

@cynthiajoan In my case I try to switch the default firebase app in my app on the fly. I have firebase app for test and for production. And I have a button to switch it. But then the app was not be created for the specified project as intended

This should be consider a bug because when we try to create the app, it should be created with projectID as a unique key, or we might try to specified more data with new AppOptions. And for the current behaviour the name is misleading. It should be GetOrCreate instead of plain Create