mcharmas / Android-ReactiveLocation

Small library that wraps Google Play Service API in brilliant RxJava Observables reducing boilerplate to minimum.
2.11k stars 312 forks source link

Calling thread must be a prepared Looper thread #164

Closed muradmohdzain closed 6 years ago

muradmohdzain commented 6 years ago

I always getting this error when running in background service.

Here is my code how i initialize :

LocationRequest request = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                .setFastestInterval(mLocationFastestInterval)
                .setInterval(mLocationInterval)
                .setSmallestDisplacement(mLocationSmallestDisplacement);

 mAddressObservable = mLocationRepository.getLocAddress(request)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());

Subscription t = mAddressObservable
                .share()
                .subscribe(new Action1<LocationModel>() {
                    @Override
                    public void call(final LocationModel loc) {                                           

                            handleLocation(loc.getLocation(), loc.getAddress().get(0));

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        handleError(throwable);                     

                    }
                });

        mSubscription.add(t);
mcharmas commented 6 years ago

Ss far as I know you cannot connect to Google Play Services from thread that has no Looper and Threads in Schedulers.io() don't have it. You would have to prepare thread with looper and pass it as different scheduler in .subscribeOn() method.

This is not an issue of this library but the behaviour of underlaying Google Play Services.