- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在某些情况下,例如 Home Widget (AppWidgetProvider
),我无权访问 Activity
或 Fragment
。
通常,我使用ProcessLifecycleOwner.get()
,或者下面的LifeCycleOwner
来观察LiveData
。
public enum ForeverStartLifecycleOwner implements LifecycleOwner {
INSTANCE;
private final LifecycleRegistry mLifecycleRegistry;
ForeverStartLifecycleOwner() {
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
}
在大多数情况下,在 LiveData
的回调中,我会尝试通过使用 从进一步观察
.LiveData
中删除 LifeCycleOwner
liveData.removeObserver
但是,有些情况
在这种情况下,会不会造成资源泄露?例如,GC 注意到一个长生命周期的 LifeCycleOwner
正在观察 LiveData A
。尽管 LiveData
A 已经超出范围,但 GC 不会释放 LiveData A
,因为长期存在的 LifeCycleObserver
仍在观察它?
如果是这样,我该如何解决这种泄漏?
最佳答案
will it cause resource leakage?
回答:我不认为这种情况会导致内存泄漏。
为什么?
因为,正如我们所见,一旦 LiveData
超出任何 LifecyclerOwner
的范围,如果您将 LifecycleOwner
设置为DESTROYED 状态。
查看observe()
方法文档;
Adds the given observer to the observers list within the lifespan of the given owner. The events are dispatched on the main thread. If
LiveData
already has data set, it will be delivered to the observer.The observer will only receive events if the owner is in STARTED or RESUMED state (active).
If the owner moves to the DESTROYED state, the observer will automatically be removed.
When data changes while the owner is not active, it will not receive any updates. If it becomes active again, it will receive the last available data automatically.
LiveData
keeps a strong reference to the observer and the owner as long as the givenLifecycleOwner
is not destroyed. When it is destroyed,LiveData
removes references to the observer & the owner.If the given owner is already in DESTROYED state,
LiveData
ignores the call.If the given owner, observer tuple is already in the list, the call is ignored. If the observer is already in the list with another owner,
LiveData
throws anIllegalArgumentException
.
因此,根据上述文档,从行开始:
如果所有者移动到 DESTROYED 状态,则观察者将自动被移除。
如果给定的所有者已经处于 DESTROYED 状态,LiveData 忽略电话。
很明显,如果您的 LifecycleOwner
没有范围(已经处于 DESTROYED 状态),那么 LiveData
会删除它的强引用,因此没有机会内存泄漏。
how can I resolve this kind of leakage? & get callback all the time:
回答:您已经创建了自己的LifecycleOwner
,所以我建议您自己处理。让您的 LiveData
成为 observeForever()
并在您的 LifecycleOwner 之后自行处理移除(
达到 DESTROYED 状态。removeObserver()
)
这不会导致内存泄漏。因为它在文档中说明:(这意味着给定的观察者将接收所有事件并且永远不会被自动删除)
Adds the given observer to the observers list. This call is similar to
observe(LifecycleOwner, Observer)
with a LifecycleOwner, which is always active. This means that the given observer will receive all events and will never be automatically removed. You should manually callremoveObserver(Observer)
to stop observing thisLiveData
. WhileLiveData
has one of such observers, it will be considered as active.If the observer was already added with an owner to this
LiveData
,LiveData
throws anIllegalArgumentException
.
这将帮助您始终接收回调,您只需要处理完成后如何删除回调。
希望对您有所帮助!
关于android - 我长寿的LifeCycleOwner一直观察LiveData会不会造成资源泄露?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52621354/
有人告诉我,如果我只有一个“东西”,比如家(不是多个家),我应该在 routes.rb 中使用资源 :home,而不是资源 :home。但是当我查看路由时,POST 函数似乎想要 home#creat
Activity 开始。这些代码框架顺利通过。 // Initialize array adapters. One for already paired devices and //
资源 search-hadoop.com search-hadoop.com索引所有邮件列表,非常适合历史搜索。当你遇到问题时首先在这里搜索,因为很可能有人已经遇到了你的问题。 邮件列表 在A
我是 WPF 的新手,正在努力使用位于单独程序集中的样式。这就是我正在做的:- 我有一个带有\Themes 文件夹的类库项目,其中包含一个“generic.xaml”,它合并了\Themes 内的子文
我正在编写一个使用虚拟树状文件结构的插件。基本上它就像一个包含文件的标准文件系统,区别在于这些文件实际上并不存在于文件系统中的特定位置,而只是 java 对象。 这些当前由使用 SettingProv
如果我在 XAML 中使用以下内容,我会收到错误消息: 错
我正在使用 laravel 资源来获取 api 的数据: return [ 'id' => $this->id, 'unread' =>
我有以下 pom.xml: 4.0.0 mycompany resource-fail 0.0.1-SNAPSHOT BazBat
许多GDI +类都实现IDisposable,但是我不确定何时应该调用Dispose。对于使用new或静态方法(例如Graphics.CreateGraphics)创建的实例来说,这很明显。但是,由属
我正在构建一组 RESTful 资源,其工作方式如下:(我将使用“people”作为示例): 获取/people/{key} - 返回一个人对象 (JSON) GET/people?first_nam
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一个使用 $resource 的简单 Controller : var Regions = $resource('mocks/regions.json'); $scope.regions =
在 Azure 门户中,如何查看不同资源之间的依赖关系。我特别想查看哪些资源正在使用我要删除的存储。 最佳答案 您可以使用应用程序洞察应用程序 map 来执行此操作: 您还可以打开存储帐户的日志记录:
我正在使用 ionic 生成资源(图标和启动画面)。我正在使用 ionic v2.1.0 和 cordova v6.4.0。 到目前为止我一直在使用(它在以前的版本中工作): cordova plat
是否可以使用 Assets 包含子文件夹中的文件? 示例:[base_url]/assets/css/pepper-grinder/jquery-ui-1.8.11.custom.min.css 最佳
我正在阅读一些尝试教授 Android 开发的书。在书中,作者概述了 res/下的一些目录。他提到 res/menu 包含基于 XML 的菜单规范。他还提到了保存“通用文件”的 res/raw。当我创
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在服务器上使用 express-resource。在我的 AngularJS Controller 中: var User = $resource('/services/users/:use
因此,每当我运行我的应用程序时,它都会立即崩溃并给出以下错误: No package identifier when getting value for resource number 0x00000
对于我正在创建的(网络)应用程序,我需要使用基本身份验证在我的 UIWebView 中加载页面。 现在设置我使用的授权 header : NSString *result = [NSString st
我是一名优秀的程序员,十分优秀!