liujingxing / rxlife Goto Github PK
View Code? Open in Web Editor NEW一行代码解决RxJava 内存泄漏,一款轻量级别的RxJava生命周期管理库
License: Apache License 2.0
一行代码解决RxJava 内存泄漏,一款轻量级别的RxJava生命周期管理库
License: Apache License 2.0
当使用kotlin语言开发的时候 as 是关键词,不能作为方法名使用,需要as
这样使用,不太方便,望改名。
rxLifeScope.launch { } 可以像 GlobalScope.launch (Dispatchers.IO){ },可以选择在哪个线程中调用吗?
有时候一些操作必须要到IO线程才能操作
使用rxjava3发现 Observable 没有as方法
onFinally中没有判断isActive,那在onFinally中操作了界面,一旦fragment被销毁回收,就会报错说xxxx until the Fragment is attached to the FragmentManager
//注:不能使用new 关键字创建MyViewModel对象,否则MyViewModel感知不到生命周期
val viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
大神请教,viewModel的创建,在kt下如上写法,在java下必须要new,上述说不能使用new,请问java下viewModel如何创建?
hi
使用.to操作符后 ,crash
2021-12-16 15:16:50.071 18042-18042/com.tpv.rain.rxjava3test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tpv.rain.rxjava3test, PID: 18042
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
at com.rxjava.rxlife.ObjectHelper.requireNonNull(ObjectHelper.kt)
at com.rxjava.rxlife.ObservableLife.subscribe(ObservableLife.java:64)
at com.tpv.rain.rxjava3test.SecondActivity.testLeak(SecondActivity.java:36)
at com.tpv.rain.rxjava3test.SecondActivity.onResume(SecondActivity.java:29)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6766)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/com.tpv.rain.rxjava3test-2/base.apk"],nativeLibraryDirectories=[/data/app/com.tpv.rain.rxjava3test-2/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.rxjava.rxlife.ObjectHelper.requireNonNull(ObjectHelper.kt)
at com.rxjava.rxlife.ObservableLife.subscribe(ObservableLife.java:64)
at com.tpv.rain.rxjava3test.SecondActivity.testLeak(SecondActivity.java:36)
at com.tpv.rain.rxjava3test.SecondActivity.onResume(SecondActivity.java:29)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6766)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
配置:
//RxJava的依赖包
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
//RxAndroid的依赖包
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation 'com.github.liujingxing.rxlife:rxlife-rxjava3:2.2.1'
code(java):
@OverRide
protected void onResume() {
super.onResume();
testLeak();
}
private void testLeak(){
Observable.timer(1, TimeUnit.SECONDS)
.to(RxLife.to(this))
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Long aLong) {
LogUtil.d(TAG, "[testLeak] onNext:"+aLong);
}
@Override
public void onError(@NonNull Throwable e) {
LogUtil.d(TAG, "[testLeak] onError");
}
@Override
public void onComplete() {
LogUtil.d(TAG, "[testLeak] onComplete");
}
});
}
fun observableByVM(view: View) {
//注:不能使用new 关键字创建MyViewModel对象,否则MyViewModel感知不到生命周期
val viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
}
这个崩溃了
Observable
.interval(delay, peroid, TimeUnit.MILLISECONDS)
.as(RxLife.as(this))
.subscribe(aLong -> {
........
});
会不执行
rxlife-rxjava2_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16.会报这个错误。但是使用implementation 'com.rxjava.rxlife:rxlife-x:1.0.8'就没有问题,求大神指导
2020-12-29 16:51:22.082 18675-18675/com.aofa.baseplatform E/ARouter::: InstantRun support error, com.android.tools.fd.runtime.Paths
2020-12-29 16:51:24.147 18675-1835/com.aofa.baseplatform E/cutils-trace: Error opening trace file: No such file or directory (2)
2020-12-29 16:51:59.199 18675-18675/com.aofa.baseplatform E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aofa.baseplatform, PID: 18675
java.lang.ClassCastException: androidx.lifecycle.LifecycleCoroutineScopeImpl cannot be cast to com.rxlife.coroutine.RxLifeScope
at androidx.lifecycle.RxLifeKt.getRxLifeScope(RxLife.kt:30)
at androidx.lifecycle.RxLifeKt.getRxLifeScope(RxLife.kt:24)
at com.aofa.baseplatform.ui.volunteer.VolunteerRegActivity.submit(VolunteerRegActivity.kt:83)
at com.aofa.baseplatform.ui.volunteer.VolunteerRegActivity$init$$inlined$apply$lambda$1.onClick(VolunteerRegActivity.kt:46)
at com.aofa.baseplatform.base.BaseActionBarWidget.onClick(BaseActionBarWidget.kt:282)
at android.view.View.performClick(View.java:7455)
at android.view.View.performClickInternal(View.java:7432)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28313)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7661)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:594)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
大佬请教一下,这个在Service里面如何跟生命周期绑定呢。
.takeUntil(new Predicate() {
@OverRide
public boolean test(Erweimalogin integer) throws Exception {
return (integer.getCode() == 1);
// 返回true时,就停止发送事件
}
})
.subscribeOn(Schedulers.io()) // 切换到IO线程进行网络请求
.observeOn(AndroidSchedulers.mainThread()) // 切换回到主线程 处理请求结果
.to(RxLife.to(this))
.subscribe(new Observer<Erweimalogin>() {
@Override
public void onSubscribe(Disposable d) {
Log.d(TAG, "dingyue");
}
@Override
public void onNext(Erweimalogin result) {
// e.接收服务器返回的数据
Log.i(TAG, String.valueOf(result.getMsg()));
}
@Override
public void onError(Throwable e) {
// 获取轮询结束信息
Log.d(TAG, e.toString());
}
@Override
public void onComplete() {
Log.d(TAG, "完成");
}
});
部分代码
自定义view,经常性的报这个错误,有什么好的方式避免么?
com.rxjava.rxlife.OutsideScopeException: View is not attached!
at com.rxjava.rxlife.ViewScope.onScopeStart(ViewScope.java:45)
at com.rxjava.rxlife.AbstractLifecycle.addObserverOnMain(AbstractLifecycle.java:58)
at com.rxjava.rxlife.AbstractLifecycle.addObserver(AbstractLifecycle.java:34)
at com.rxjava.rxlife.LifeObserver.onSubscribe(LifeObserver.java:29)
at io.reactivex.internal.operators.observable.ObservableDetach$DetachObserver.onSubscribe(ObservableDetach.java:66)
demo中的presenter直接to(RxLife.to(owner)) 不可以么?就下面这样子:
public class Presenter{
public Presenter(LifecycleOwner owner) {
Observable.interval(1, 1, TimeUnit.SECONDS)
.to(RxLife.to(owner)) //这里的this 为Scope接口对象
.subscribe(aLong -> {
Log.e("LJX", "accept aLong=" + aLong);
});
}
}
请问这样会出现什么问题么?
LifeObserver 类捕获异常的问题,正常逻辑,客户端一般是接口相关的错误,才会捕获异常,结果现在一些代码问题导致的异常也会被捕获。
比方说接口请求成功了,我在setData的代码里面,使用导致什么空指针,也会被这里catch。
目前项目线上的异常都被它捕获了,拦截了一些crash问题,这里希望开源作者能优化下,不要捕获全部的异常
@Override
public void onNext(T t) {
if (isDisposed()) return;
try {
downstream.onNext(t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
get().dispose();
onError(e);
}
}
LifecycleScope 为什么要在 onScopeStart 方法中执行 onScopeEnd 去主动移除 observer 监听,然后重新添加?
在使用的过程中使用as方法转换后返回ObservableLife对象然后调用subscribe方法,这样的流程可以走得通,但是这样导致了扩展性的限制, 我在实际使用中想把这个方法抽取到网络请求的公共方法中,这样对代码的侵入性会很低,但是由于调用该方法之后只能 subscribe 导致将这个方法抽取比较复杂了, 所以我想问一下为什么不能返回 Observable 对象方便向下调用呢?
看了作者添加了androidX支持,但是原始android包又报错
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.