使用Retrofit和RxJava的生命周期管理
来源:互联网 发布:js开发日历时间控件 编辑:程序博客网 时间:2024/05/16 14:48
Activity
, for instance, while a background thread is still running. The thread will attempt to access views on the now dead Activity
, which results in a crash. (This will also create a memory leak, since your background thread holds on to the Activity
even though it’s not visible anymore.)This is still a concern when using RxJava on Android, but you can deal with the problem in a more elegant way by using Subscription
s and a number of Observable operators. In general, when you run an Observable
inside an Activity
that subscribes to the result (either directly or through an inner class), you must unsubscribe from the sequence in onDestroy
, as shown in the following example:
// MyActivityprivate Subscription subscription;protected void onCreate(Bundle savedInstanceState) { this.subscription = observable.subscribe(this);}...protected void onDestroy() { this.subscription.unsubscribe(); super.onDestroy();}
This ensures that all references to the subscriber (the Activity
) will be released as soon as possible, and no more notifications will arrive at the subscriber through onNext
.
One problem with this is that if the Activity
is destroyed because of a change in screen orientation, the Observable will fire again in onCreate
. You can prevent this by using the cache
or replay
Observable operators, while making sure the Observable somehow survives theActivity
life-cycle (for instance, by storing it in a global cache, in a Fragment, etc.) You can use either operator to ensure that when the subscriber subscribes to an Observable that’s already “running,” items emitted by the Observable during the span when it was detached from theActivity
will be “played back,” and any in-flight notifications from the Observable will be delivered as usual.
- 使用Retrofit和RxJava的生命周期管理
- 使用 RxJava 和 Retrofit
- Rxjava 和 Retrofit 的封装使用
- Retrofit和RxJava的结合使用
- Rxjava和Retrofit结合使用的案例
- Retrofit和Rxjava的简单使用
- Retrofit 和 Rxjava 的结合使用
- Retrofit和Rxjava的简单使用
- 结合retrofit和rxjava的使用
- Retrofit+RxJava的使用
- Retrofit+RXJava的使用
- 【RxJava】RxJava的生命周期管理
- RxBus和RxManager管理rxJava的生命周期
- Retrofit 和 RxJava 结合使用
- Retrofit和RxJava结合使用
- RxJava和Retrofit结合使用
- RxJava和Retrofit结合使用
- rxjava 和retrofit 结合使用
- IOS性能调优系列:使用Instruments动态分析内存泄漏,调优instruments
- 自定义注解
- iOS把所有界面的状态栏的字体颜色都设置为白色
- ANT教程经典
- github学习笔记
- 使用Retrofit和RxJava的生命周期管理
- 沉舟侧畔千帆过,病树前头万木春
- Instruments- time profiler
- Java 数组、方法、基础笔记
- sass初学练习二
- 一天中的48小时
- 51、微信-发送朋友圈ShareActivity
- Xshell 更改字体配色
- ifconfig命令详解