5

Google マップを使用し、カメラの変更をリッスンするアプリケーションがあります。私の問題は、カメラを変更するたびにバックエンドをリクエストする必要があることです。私がやりたいことは、RxAndroid/Java を使用してデバウンスすることで、リクエストの数を制限することです。

私のコードは次のようになります。

Observable.create(new Observable.OnSubscribe<CameraPosition>() {
        @Override
        public void call(Subscriber<? super CameraPosition> subscriber) {
            if (!subscriber.isUnsubscribed()) {
                map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                    @Override
                    public void onCameraChange(CameraPosition cameraPosition) {
                        subscriber.onNext(cameraPosition);
                    }
                });
            }
        }
    }).subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .onErrorResumeNext(Observable.<CameraPosition>empty())
            .debounce(1, TimeUnit.SECONDS)
            .subscribe(cameraPosition -> {
                final LatLngBounds item = map.getProjection().getVisibleRegion().latLngBounds;

                homeActionBarActivity.getNMB().getRide().list(
                        item.southwest.latitude,
                        item.southwest.longitude,
                        item.northeast.latitude,
                        item.northeast.longitude)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .onErrorResumeNext(Observable.<List<Ride>>empty())
                        .subscribe(new Action1<List<Ride>>() {
                            @Override
                            public void call(List<Ride> rides) {

                                clearRidePointsFromMap();

                                for (Ride ride : rides) {
                                    if (isAdded())
                                        addRideStartPointToMap(ride);
                                }

                            }
                        });
            });

ご覧のとおり、両方 (subscribeOn とobserveOn) に MainThread を使用することを強制していますが、「メイン スレッドではありません」というエラーが引き続き表示されます。

ここでスタックトレース:

01-27 10:59:24.049    3049-3066/com.nousmotards.android E/AndroidRuntime﹕ FATAL EXCEPTION: RxComputationThreadPool-2
    Process: com.nousmotards.android, PID: 3049
    java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
            at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:52)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:864)
     Caused by: rx.exceptions.OnErrorNotImplementedException: Not on the main thread
            at rx.Observable$31.onError(Observable.java:7134)
            at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:154)
            at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
            at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:137)
            at rx.observers.SerializedObserver.onNext(SerializedObserver.java:159)
            at rx.observers.SerializedSubscriber.onNext(SerializedSubscriber.java:81)
            at rx.internal.operators.OperatorDebounceWithTime$DebounceState.emit(OperatorDebounceWithTime.java:128)
            at rx.internal.operators.OperatorDebounceWithTime$1$1.call(OperatorDebounceWithTime.java:72)
            at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:864)
     Caused by: java.lang.IllegalStateException: Not on the main thread
            at com.google.k.a.cl.b(Unknown Source)
            at com.google.maps.api.android.lib6.c.ca.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.el.l(Unknown Source)
            at com.google.android.gms.maps.internal.l.onTransact(SourceFile:312)
            at android.os.Binder.transact(Binder.java:361)
            at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.getProjection(Unknown Source)
            at com.google.android.gms.maps.GoogleMap.getProjection(Unknown Source)
            at com.nousmotards.android.fragments.home.MapFragment.lambda$onViewCreated$33(MapFragment.java:117)
            at com.nousmotards.android.fragments.home.MapFragment.access$lambda$0(MapFragment.java)
            at com.nousmotards.android.fragments.home.MapFragment$$Lambda$1.call(Unknown Source)
            at rx.Observable$31.onNext(Observable.java:7139)
            at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130)
            at rx.observers.SerializedObserver.onNext(SerializedObserver.java:159)
            at rx.observers.SerializedSubscriber.onNext(SerializedSubscriber.java:81)
            at rx.internal.operators.OperatorDebounceWithTime$DebounceState.emit(OperatorDebounceWithTime.java:128)
            at rx.internal.operators.OperatorDebounceWithTime$1$1.call(OperatorDebounceWithTime.java:72)
            at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:864)

何か考えはありますか?

注: Observable.create(...) から map.setOnCameraChangeListerner(...) を取得すると、正しく動作します。

4

1 に答える 1