Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxJava with AutoDispose: Provoke error after life cycle ends
package dk.bnr.androidbooking;
import android.arch.lifecycle.Lifecycle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.AutoDisposePlugins;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
public class MyAcitvity extends AppCompatActivity {
private TestRxJavaLifeCycleErrorAfterStopActivity testRxJavaLifeCycleErrorAfterStopActivity;
@Override
protected void onStart() {
testRxJavaLifeCycleErrorAfterStopActivity = new TestRxJavaLifeCycleErrorAfterStopActivity(this);
super.onStart();
testRxJavaLifeCycleErrorAfterStopActivity.onStart();
}
@Override
protected void onStop() {
super.onStop();
testRxJavaLifeCycleErrorAfterStopActivity.onStop();
}
public class TestRxJavaLifeCycleErrorAfterStopActivity {
private static final String TAG = "TestRxJavaLifeCycleErro";
private final AppCompatActivity activity;
private volatile boolean stopped;
public TestRxJavaLifeCycleErrorAfterStopActivity(AppCompatActivity activity) {
this.activity = activity;
// No effect, I think this is for something else
AutoDisposePlugins.setOutsideLifecycleHandler(t -> Log.w(TAG, "Ignored exception from OutsideLifecycleHandler: " + t));
}
public void onStart() {
createObservable(activity);
}
public void onStop() {
Log.d(TAG, "onStop");
stopped = true;
}
private void createObservable(AppCompatActivity activity) {
Observable.create(e -> {
// Wait for life cycle to see my activity is stopped, then throw simulation IOException
while (!stopped) {
Log.d(TAG, "Sleeping");
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
}
throw new IOException("No AutoDispose 1");
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(activity, Lifecycle.Event.ON_STOP)))
.subscribe((it) -> {
}, (throwable -> Log.w(TAG, "Caught error " + throwable)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.