firebase-unity-sdk icon indicating copy to clipboard operation
firebase-unity-sdk copied to clipboard

FirebaseApp.Create not override default instance

Open Thaina opened this issue 5 years ago • 11 comments

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)?

  • Don't know

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

  • 100%

Thaina avatar Apr 17 '20 09:04 Thaina

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.

patm1987 avatar Apr 17 '20 19:04 patm1987

@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 avatar Apr 18 '20 04:04 Thaina

@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

Thaina avatar Apr 20 '20 08:04 Thaina

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!

DellaBitta avatar Apr 21 '20 18:04 DellaBitta

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

Thaina avatar Apr 22 '20 01:04 Thaina

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

DellaBitta avatar Apr 22 '20 15:04 DellaBitta

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

PedroMR avatar Jul 20 '20 21:07 PedroMR

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?

chkuang-g avatar Feb 11 '21 06:02 chkuang-g

@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

Thaina avatar Feb 12 '21 08:02 Thaina

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!

cynthiajoan avatar Feb 13 '21 07:02 cynthiajoan

@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

Thaina avatar Feb 13 '21 11:02 Thaina