From 4fd9d7fb2ddad48266872a8f68dabe16289bd12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=84=A7=E5=AD=90?= Date: Sun, 15 Nov 2015 16:10:25 +0800 Subject: [PATCH] use MainThreadSubscription --- .../domain/usercase/PrepareCase.java | 23 ++++--------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/domain/src/main/java/com/rxweather/domain/usercase/PrepareCase.java b/domain/src/main/java/com/rxweather/domain/usercase/PrepareCase.java index a4631a1..21ff5e6 100644 --- a/domain/src/main/java/com/rxweather/domain/usercase/PrepareCase.java +++ b/domain/src/main/java/com/rxweather/domain/usercase/PrepareCase.java @@ -10,6 +10,7 @@ import android.util.SparseArray; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.jakewharton.rxbinding.internal.MainThreadSubscription; import com.joker.rxweather.common.Constants; import com.joker.rxweather.common.rx.rxAndroid.SchedulersCompat; import com.joker.rxweather.model.entities.AddressEntity; @@ -19,11 +20,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.List; -import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import org.json.JSONObject; import rx.Observable; import rx.Subscriber; -import rx.Subscription; import rx.functions.Func1; import rx.functions.Func2; @@ -33,9 +32,6 @@ public class PrepareCase extends UseCase { private static final String TAG = PrepareCase.class.getSimpleName(); - private volatile int unsubscribed; - private static final AtomicIntegerFieldUpdater unsubscribedUpdater = - AtomicIntegerFieldUpdater.newUpdater(PrepareCase.class, "unsubscribed"); private LocationManager locationManager; private AssetManager assetManager; @@ -80,22 +76,13 @@ public void onLocationChanged(final Location location) { subscriber.onNext(location); subscriber.onCompleted(); } - locationManager.removeUpdates(this); - handlerThread.getLooper().quit(); } }; - subscriber.add(new Subscription() { - @Override public void unsubscribe() { - if (unsubscribedUpdater.compareAndSet(PrepareCase.this, 0, 1)) { - - locationManager.removeUpdates(locationListener); - handlerThread.getLooper().quit(); - } - } - - @Override public boolean isUnsubscribed() { - return unsubscribed != 0; + subscriber.add(new MainThreadSubscription() { + @Override protected void onUnsubscribe() { + locationManager.removeUpdates(locationListener); + handlerThread.getLooper().quit(); } });