Android - Firebase - Do I need to delete EVERY single listener?

I have done enough research and cannot find the answer I need.

What I know:. When I bind a ValueEventListener to a database link, I know that I need to delete it later (finding it difficult now with some massive memory leak.

What I DO NOT Know: Do I also need to disconnect all other listeners? (This includes Firebase Database, Storage, and Auth, the three APIs I use)

Example:

UploadTask uploadTask = ref.putFile(uploadFile); uploadTask.addOnFailureListener(new OnFailureListener() { //@Override code here }).addOnSuccessListener(new OnSuccessListener<UploadTask.TakeSnapshot>() { //@Override code here }).addOnProgressListner(new OnProgressListner<UploadTask.TakeSnapshot>() { //@Override code here }; 

I think this is enough to show you the meaning of what I mean. This is how my actual code is currently structured.

Questions:

  • Do I need to remove all of these listeners just in case? (system solution, the phone dies, whatever) before this callback occurs?
  • Is it possible to somehow connect them and finish them all three times because I have 30 of them in my code and really don’t feel like restructuring all this to assign all these listeners to JUST variables, so I can pass them to " removeBlahBlahBlahListener (listenerVariable) "over and over.
  • A little off topic, but I'm too lazy to move all my code from onCreate to onStart ... it's bad practice for me to remove all these listeners, finish work, end call () (or whatever it kills activity, although I think this is not guaranteed) and just recreate activity from scratch? This is a small, simple application, so the overhead of recreating activity is not a biggie. Just curious what is "right."

I suppose this is the result of poor planning and lack of knowledge (I only program for fun, not for work, unfortunately), so if I need to go a hard route, I think it's a learning experience, right?

+6
source share
1 answer

Automatic deregistration of listeners when activity is stopped is a function of the Task class in android and its derived classes (StorageTask).

This means you can do something like this:

 UploadTask uploadTask = ref.putFile(uploadFile); uploadTask.addOnFailureListener(thisActivity, new OnFailureListener() { //@Override code here }).addOnSuccessListener(thisActivity, new OnSuccessListener<UploadTask.TaskSnapshot>() { //@Override code here }).addOnProgressListner(thisActivity, new OnProgressListner<UploadTask.TaskSnapshot>() { //@Override code here }; 

You can also do this using task objects returned from a real-time database, such as setValue, as in:

 databaseReference.setValue("newValue").addOnSuccessListener(thisActivity, ...) 

To answer your questions directly:

  • Use the scope version to automatically unregister listeners when activity stops. Please note that for storage, you can request the operations to be performed when your activity begins with the use of StorageReference.getActiveUploadTasks and StorageReference.getActiveDownloadTasks and is overwritten.

  • You do not need to unsubscribe manually if you use listeners with advanced features. I don’t know how to unsubscribe from non-task listeners.

  • Well, I'm not sure how you can guarantee that the OS will always kill your task, and not stop / start it again - and how your completion code will be guaranteed. I would advise you to move the code to onStart

+5
source

All Articles