Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 860
yianchen Avatar asked Aug 16 '26 16:08

yianchen


1 Answers

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.

like image 95
waleed Avatar answered Aug 24 '26 18:08

waleed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!