简体   繁体   English

什么时候应该调用removeStickyEvent(...)?

[英]When should removeStickyEvent(…) be called?

I'm using the GreenRobot EventBus lib on Android. 我在Android上使用GreenRobot EventBus lib。

Could someone tell me, what's best practice for calling the removeStickyEvent(...)? 有人能告诉我,调用removeStickyEvent(...)的最佳做法是什么? Up-Action, back button pressed, ...? Up-Action,后退按钮,......?

Thank you guys. 感谢你们。

To answer your question about the Event object staying in memory: only the last sticky event posted stays in memory for each given type. 要回答关于保留在内存中的Event对象的问题:只有最后发布的粘性事件会在每个给定类型的内存中保留。

Take a look at the source here . 看看这里的来源。

Basically to summarize: Eventbus keeps a map of stickyEvents with the key being class type and the value being the field. 基本上总结一下:Eventbus保持stickyEvents的映射,其中键是类类型,值是字段。 So when a new sticky event is posted with EventBus.postSticky(event) the event posted overwrites the old event. 因此,当使用EventBus.postSticky(event)发布新的粘性事件时,发布的事件将覆盖旧事件。 So I think you are safe from building up many sticky events. 所以我认为你可以安全地建立许多粘性事件。

I was worried about that also. 我也很担心。 I wish it was explained better in the docs but at least its open source so we can look inside and see whats happening. 我希望在文档中更好地解释它,但至少是它的开源,所以我们可以查看内部并看到发生了什么。

I guess to answer your question more completely. 我想更完整地回答你的问题。 When should you remove a sticky event? 什么时候应该删除粘性事件? When you don't want calls to get the sticky event to not return anything. 当您不希望调用时,粘性事件不会返回任何内容。 When exactly that is is an application specific requirement. 恰当的是特定于应用程序的要求。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM