Skip to content

Commit

Permalink
Fixed IllegalArgumentException in LocationServicesOkObservableApi23Fa…
Browse files Browse the repository at this point in the history
…ctory (#573)

Due to a race condition and subscribing/unsubscribing from different threads it may have happened that unregistering was made before registering finished.
  • Loading branch information
dariuszseweryn authored May 13, 2019
1 parent d266b3d commit f9d2779
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;
import io.reactivex.schedulers.Schedulers;

@TargetApi(Build.VERSION_CODES.KITKAT)
public class LocationServicesOkObservableApi23Factory {
Expand All @@ -30,7 +31,7 @@ public class LocationServicesOkObservableApi23Factory {
public Observable<Boolean> get() {
return Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
public void subscribe(final ObservableEmitter<Boolean> emitter) {
final boolean initialValue = locationServicesStatus.isLocationProviderOk();
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -43,11 +44,14 @@ public void onReceive(Context context, Intent intent) {
context.registerReceiver(broadcastReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
public void cancel() {
context.unregisterReceiver(broadcastReceiver);
}
});
}
}).distinctUntilChanged();
})
.distinctUntilChanged()
.subscribeOn(Schedulers.trampoline())
.unsubscribeOn(Schedulers.trampoline());
}
}

0 comments on commit f9d2779

Please sign in to comment.