yianchen
yianchen

Reputation: 31

Firebase database in unity platform android unsubscribe doesn't work

The version that I am using is:

Firebase version 5.4.2 and 5.5.0 Unity version 2018.5.5f1

I am using childAdded to registering or unsubscribing are normal on the editor, but I am not able to unsubscribe after building apk, It receives the same return event as the number of subscriptions each time the data is added.

Upvotes: 3

Views: 424

Answers (1)

waleed
waleed

Reputation: 464

I was facing a similar error. I had these two functions that were working fine in unity editor but didn't work on an Android device using APK.

private void OnEnable ()
{
    FirebaseDatabase.DefaultInstance.GetReference("/currentGame").ValueChanged += HandleComingGameChanges;
}

private void OnDisable ()
{
    FirebaseDatabase.DefaultInstance.GetReference("/currentGame").ValueChanged -= HandleComingGameChanges;
}

So i just added a DatabaseReference variable and subscribe to that OnEnable function and unsubscribe to that on GameObject-Disable function and it starts working.

Here is the pseudo code of this.

private DatabaseReference reference;
private void Awake ()
{
    reference = FirebaseDatabase.DefaultInstance.GetReference("/myReference");
}
private void OnEnable ()
{
    reference.ValueChanged += HandleChanges;
}

private void OnDisable ()
{
    reference.ValueChanged -= HandleChanges;
}

Note: I'm initializing reference variable in Awake function because it is called before OnEnable and Start is called after OnEnable.

Upvotes: 3

Related Questions