rx main thread observe bug fix
This commit is contained in:
parent
0a6c4f1271
commit
d1d9055ba7
|
|
@ -25,12 +25,10 @@ dependencies {
|
|||
|
||||
compile 'net.danlew:android.joda:2.9.4.1'
|
||||
compile 'com.android.support:multidex:1.0.1'
|
||||
compile 'io.reactivex:rxandroid:1.2.1'
|
||||
|
||||
provided 'com.android.support:appcompat-v7:24.2.1'
|
||||
|
||||
provided 'net.danlew:android.joda:2.9.3'
|
||||
provided 'io.reactivex:rxandroid:1.2.1'
|
||||
|
||||
provided 'com.squareup.retrofit2:retrofit:2.1.0'
|
||||
provided 'com.squareup.okhttp3:okhttp:3.4.1'
|
||||
provided('com.google.http-client:google-http-client-jackson2:1.22.0') {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ package ru.touchin.templates;
|
|||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.multidex.MultiDex;
|
||||
|
|
@ -29,6 +30,8 @@ import com.crashlytics.android.Crashlytics;
|
|||
|
||||
import net.danlew.android.joda.JodaTimeAndroid;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.fabric.sdk.android.Fabric;
|
||||
import ru.touchin.roboswag.components.navigation.fragments.ViewControllerFragment;
|
||||
import ru.touchin.roboswag.components.views.TypefacedEditText;
|
||||
|
|
@ -39,6 +42,13 @@ import ru.touchin.roboswag.core.log.LcGroup;
|
|||
import ru.touchin.roboswag.core.log.LcLevel;
|
||||
import ru.touchin.roboswag.core.log.LogProcessor;
|
||||
import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
|
||||
import rx.Scheduler;
|
||||
import rx.Subscription;
|
||||
import rx.android.plugins.RxAndroidPlugins;
|
||||
import rx.android.plugins.RxAndroidSchedulersHook;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.functions.Action0;
|
||||
import rx.subscriptions.Subscriptions;
|
||||
|
||||
/**
|
||||
* Created by Gavriil Sitnikov on 10/03/16.
|
||||
|
|
@ -65,6 +75,12 @@ public abstract class TouchinApp extends Application {
|
|||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() {
|
||||
@Override
|
||||
public Scheduler getMainThreadScheduler() {
|
||||
return new MainThreadScheduler();
|
||||
}
|
||||
});
|
||||
JodaTimeAndroid.init(this);
|
||||
if (isDebug()) {
|
||||
ViewControllerFragment.setInDebugMode();
|
||||
|
|
@ -106,4 +122,47 @@ public abstract class TouchinApp extends Application {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* This hacky class is needed to execute actions immediately on main thread but not schedule on main thread handler with 0 delay instead.
|
||||
*/
|
||||
private static class MainThreadScheduler extends Scheduler {
|
||||
|
||||
@Override
|
||||
public Worker createWorker() {
|
||||
return new WrapperMainThreadWorker();
|
||||
}
|
||||
|
||||
private class WrapperMainThreadWorker extends Worker {
|
||||
|
||||
@NonNull
|
||||
private final Worker parentWorker = AndroidSchedulers.from(Looper.getMainLooper()).createWorker();
|
||||
|
||||
@Override
|
||||
public Subscription schedule(@NonNull final Action0 action) {
|
||||
if (Looper.getMainLooper().equals(Looper.myLooper())) {
|
||||
action.call();
|
||||
return Subscriptions.unsubscribed();
|
||||
}
|
||||
return parentWorker.schedule(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Subscription schedule(@NonNull final Action0 action, final long delayTime, @NonNull final TimeUnit unit) {
|
||||
return parentWorker.schedule(action, delayTime, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unsubscribe() {
|
||||
parentWorker.unsubscribe();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUnsubscribed() {
|
||||
return parentWorker.isUnsubscribed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue