Android-Firebase-我需要删除每个监听器吗? [英] Android - Firebase - Do I need to remove EVERY single listener?

查看:134
本文介绍了Android-Firebase-我需要删除每个监听器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了相当多的研究,找不到我需要的答案.

I did a reasonable amount of research and can not find the answer I need.

我所知道的:当我将ValueEventListener附加到数据库引用时,我知道以后需要删除它(现在很难解决,因为这会造成大量内存泄漏.

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.

我不知道的事情:我还需要分离所有其他侦听器吗? (这包括Firebase数据库,存储和Auth,即我正在使用的三个API)

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)

示例:

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.

问题:

  1. 我是否需要删除所有这些监听器,以防活动 在此之前终止(系统决定,电话死机等) 回调发生了吗?
  2. 我可以将它们捆绑在一起,然后一次终止所有三个 因为我的代码中有30种这样的东西,但真的感觉不到 就像对所有这些结构进行重组以分配所有这些侦听器 变量,所以我可以将它们传递给 一遍又一遍"removeBlahBlahBlahListener(listenerVariable)".
  3. 稍微偏离主题,但是,我懒得将所有代码从 onCreate to onStart ...是我删除所有这些的不好的做法 侦听器,完成操作,调用finish()(或其他任何方法) 杀死一个活动,尽管我猜不能保证),然后从头开始重新创建活动? 这是一个小型的简单应用程序,因此重新创建活动的开销 没什么大不了的.只是好奇什么是正确的".
  1. Do I need to remove all of those listeners just in case the activity is terminated (system decision, phone dies, whatever) before that callback happens?
  2. Can I bundle them up somehow and terminate them all three at once because I have like 30 of these in my code and really don't feel like restructuring all of it in order to assign all these listeners to variables JUST so I can pass them into the "removeBlahBlahBlahListener(listenerVariable)" over and over.
  3. Slightly off topic, but, I am too lazy to move all my code from onCreate to onStart... is is bad practice for me to remove all these listeners, finish things up, call finish() (or whatever it is that kills off an activity, although I guess this is not guaranteed) and just recreate the activity from scratch? It's a small simple app so the overhead of re-creating the activity is no biggie. Just curious what is "right".

我想这只是计划不足和知识不足的结果(我只是为了娱乐而不是为了工作而编程),所以如果我必须走艰难的道路,我想那是一种学习经验,对吗?

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?

推荐答案

在活动停止时自动注销侦听器是android及其派生类(StorageTask)中的任务"类的功能.

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
};

您还可以使用从实时数据库返回的Task对象(例如setValue)执行此操作,

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:

  1. 使用活动范围的版本在活动停止时自动注销注册侦听器.请注意,对于存储,您可以在活动开始时使用StorageReference.getActiveUploadTasks和StorageReference.getActiveDownloadTasks并重新订阅来查询正在运行的操作.

  1. 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.

好吧,我不确定如何保证操作系统将始终终止您的任务,而不是再次停止/重新启动它-以及如何保证完成代码的运行.我建议您将代码移至onStart

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

这篇关于Android-Firebase-我需要删除每个监听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆