Search code examples
androidfirebaselistenerweak-referencesweak

Does Firebase use `WeakReference` for their listeners on Android?


I want to know if Firebase uses the WeakReference class for references to listener instances.

Imagine the following scenario: A silly developer attaches a ValueEventListenerin onCreate() of his SillyActivity and forgets to remove it. Does the listeners reference get invalid if the SillyActivity get's killed? Otherwise it would remain in the process heap and even keep being executed.

In other words: Being a WeakReference, it wouldn't be a disaster for the listener not to get removed if it's only added once in the lifecycle of an activity/fragment. It wouldn't leak over the lifetime of the activity/fragment.

Another related bonus question is, if listeners added with addListenerForSingleValueEvent() get removed automatically. The documentation claims to fire the listener once but not to remove it. I think it get's removed automatically - this would be the easiest way to achieve a one time fire.


Solution

  • I doubt it, otherwise it wouldn't work with things like this:

    polls.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
        ...
      }
    }
    

    Since there wouldn't be any non-WeakReferences to the listener.