I did a reasonable amount of research and can not find the answer I need.
What I DO know: When I attach a ValueEventListener to a database reference, I know I need to remove it later (finding that out the hard way now with some massive memory leakage.
What I DON'T know: Do I also need to detach all other listeners? (This is to include Firebase Database, Storage, and Auth, the three APIs I am using)
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 that's enough to show you the point of what I mean. This is how my actual code is currently structured.
Questions:
I imagine this is just a result of poor planning and lack of knowledge (I only program for fun, not for a job unfortunately) so if I have to take the hard route I guess it's a learning experience, right?
Firebase utilizes listeners to watch for changes in a specified node. It is similar to an event handler in the sense that a code is triggered based on a certain circumstance. In our case, whenever changes in that node's data occur, the listener automatically provides the application updated data, called a snapshot.
Stay organized with collections Save and categorize content based on your preferences. Classes implementing this interface can be used to receive events about data changes at a location. Attach the listener to a location user addValueEventListener(ValueEventListener) .
Firebase is a real time data base which allows to store tree of lists of objects. It allows to synchronize data between different devices. It is a NoSQL JSON database.
Auto unregistering listeners when an activity stops is a feature on the class "Task" 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 with Task objects returned from the realtime Database such as setValue as in:
databaseReference.setValue("newValue").addOnSuccessListener(thisActivity, ...)
So to answer your questions directly:
Use the activity scoped version to automatically unregister the listeners on activity stop. Note that for storage, you can query running operations when your activity starts using StorageReference.getActiveUploadTasks and StorageReference.getActiveDownloadTasks and re-subscribe.
You shouldn't need to unsubscribe manually if using scoped listeners. I do not know of a way to batch unsubscribe to non-task based listeners.
Well I'm not sure how you can guarantee that the OS will always kill your task instead of stopping/starting it again -- and how your finish code will be guaranteed to run. I would advise you move the code to onStart
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With