eurosecom
eurosecom

Reputation: 2992

Android RxFirebaseDatabase delaying subscription

I use RxFirebaseDatabase by https://github.com/ezhome/Android-RxFirebase. How do I delay subscription of observeValueEvent ? My code

Fragment.java

 Query fbQuery = firebaseRef.child("fireblog");
 RxFirebaseDatabase.getInstance()
          .observeValueEvent(fbQuery)
          .subscribe(getPostsSubscriber);

RxFirebaseDatabase.java method observeValueEvent

public Observable<DataSnapshot> observeValueEvent(final Query firebaseRef) {
    return Observable.create(new Observable.OnSubscribe<DataSnapshot>() {
        @Override
        public void call(final Subscriber<? super DataSnapshot> subscriber) {
            final ValueEventListener listener =
                    firebaseRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            subscriber.onNext(dataSnapshot);
                        }

                        @Override
                        public void onCancelled(DatabaseError error) {
                            FirebaseDatabaseErrorFactory.buildError(subscriber, error);
                        }
                    });

            // When the subscription is cancelled, remove the listener
            subscriber.add(Subscriptions.create(new Action0() {
                @Override
                public void call() {
                    firebaseRef.removeEventListener(listener);
                }
            }));
        }
    })
            .delay(300, TimeUnit.MILLISECONDS)
            .compose(this.<DataSnapshot>applyScheduler());
}

applyScheduler method

@SuppressWarnings("unchecked") private <T> Observable.Transformer<T, T> applyScheduler() {
return new Observable.Transformer<T, T>() {
  @Override public Observable<T> call(Observable<T> observable) {
    if (observeOnScheduler != null) {
      return observable.observeOn(observeOnScheduler);
    }
    return observable;
  }
    };
  }

I get Fatal Exception thrown on Scheduler.Worker thread. Can you help me please ?

logcat

   05-08 20:42:00.098 8366-8749/com.eusecom.attendance E/UncaughtException: java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                                                                         at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)
                                                                         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                         at java.lang.Thread.run(Thread.java:841)
                                                                      Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
                                                                         at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:187)
                                                                         at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
                                                                         at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:216)
                                                                         at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
                                                                         at rx.internal.operators.OperatorDelay$1$3.call(OperatorDelay.java:87)
                                                                         at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189)
                                                                         at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                         at java.lang.Thread.run(Thread.java:841) 
                                                                      Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
                                                                         at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:187) 
                                                                         at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115) 
                                                                         at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:216) 
                                                                         at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139) 
                                                                         at rx.internal.operators.OperatorDelay$1$3.call(OperatorDelay.java:87) 
                                                                         at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189) 
                                                                         at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
                                                                         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                         at java.lang.Thread.run(Thread.java:841) 
                                                                      Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
                                                                         at android.util.Log.getStackTraceString(Log.java:395)
                                                                         at android.util.Log.e(Log.java:315)
                                                                         at com.google.firebase.crash.internal.zzh.uncaughtException(Unknown Source)
                                                                         at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
                                                                         at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
                                                                         at rx.internal.schedulers.ScheduledAction.signalError(ScheduledAction.java:68)
                                                                         at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)
                                                                         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                         at java.lang.Thread.run(Thread.java:841) 
                                                                      Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                         at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6809)
                                                                         at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1110)
                                                                         at android.view.ViewGroup.invalidateChild(ViewGroup.java:4484)
                                                                         at android.view.View.invalidate(View.java:11396)
                                                                         at android.view.View.invalidateParentIfNeeded(View.java:11482)
                                                                         at android.view.View.clearAnimation(View.java:17129)
                                                                         at com.eusecom.attendance.PostsFragment.showProgress(PostsFragment.java:220)
                                                                         at com.eusecom.attendance.PostsFragment.renderBlogPosts(PostsFragment.java:208)
                                                                         at com.eusecom.attendance.PostsFragment.access$600(PostsFragment.java:41)
                                                                         at com.eusecom.attendance.PostsFragment$GetPostsSubscriber.onNext(PostsFragment.java:258)
                                                                         at com.eusecom.attendance.PostsFragment$GetPostsSubscriber.onNext(PostsFragment.java:237)
                                                                         at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:134)
                                                                         at rx.internal.operators.OperatorDelay$1$3.call(OperatorDelay.java:87)
                                                                         at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189)
                                                                         at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                         at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                         at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                         at java.lang.Thread.run(Thread.java:841) 
                                                                      Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                         at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6809)
                                                                         at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1078)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                         at android.view.View.requestLayout(View.java:16775)
                                                                        at android.view.View.requestLayout(View.ja
 05-08 20:42:00.158 8366-8568/com.eusecom.attendance I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:6 and remote module com.google.android.gms.tagmanager:9
 05-08 20:42:00.158 8366-8568/com.eusecom.attendance I/DynamiteModule: Selected remote version of com.google.android.gms.tagmanager, version >= 9
 05-08 20:42:00.399 8366-8749/com.eusecom.attendance E/AndroidRuntime: FATAL EXCEPTION: RxComputationScheduler-1
                                                                  java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                                                                      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)
                                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                      at java.lang.Thread.run(Thread.java:841)
                                                                   Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
                                                                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:187)
                                                                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
                                                                      at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:216)
                                                                      at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
                                                                      at rx.internal.operators.OperatorDelay$1$3.call(OperatorDelay.java:87)
                                                                      at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189)
                                                                      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                      at java.lang.Thread.run(Thread.java:841) 
                                                                   Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
                                                                      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:187) 
                                                                      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115) 
                                                                      at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:216) 
                                                                      at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139) 
                                                                      at rx.internal.operators.OperatorDelay$1$3.call(OperatorDelay.java:87) 
                                                                      at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189) 
                                                                      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
                                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                      at java.lang.Thread.run(Thread.java:841) 
                                                                   Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
                                                                      at android.util.Log.getStackTraceString(Log.java:395)
                                                                      at android.util.Log.e(Log.java:315)
                                                                      at com.google.firebase.crash.internal.zzh.uncaughtException(Unknown Source)
                                                                      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
                                                                      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
                                                                      at rx.internal.schedulers.ScheduledAction.signalError(ScheduledAction.java:68)
                                                                      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)
                                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                      at java.lang.Thread.run(Thread.java:841) 
                                                                   Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                      at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6809)
                                                                      at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1110)
                                                                      at android.view.ViewGroup.invalidateChild(ViewGroup.java:4484)
                                                                      at android.view.View.invalidate(View.java:11396)
                                                                      at android.view.View.invalidateParentIfNeeded(View.java:11482)
                                                                      at android.view.View.clearAnimation(View.java:17129)
                                                                      at com.eusecom.attendance.PostsFragment.showProgress(PostsFragment.java:220)
                                                                      at com.eusecom.attendance.PostsFragment.renderBlogPosts(PostsFragment.java:208)
                                                                      at com.eusecom.attendance.PostsFragment.access$600(PostsFragment.java:41)
                                                                      at com.eusecom.attendance.PostsFragment$GetPostsSubscriber.onNext(PostsFragment.java:258)
                                                                      at com.eusecom.attendance.PostsFragment$GetPostsSubscriber.onNext(PostsFragment.java:237)
                                                                      at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:134)
                                                                      at rx.internal.operators.OperatorDelay$1$3.call(OperatorDelay.java:87)
                                                                      at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:189)
                                                                      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153) 
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                      at java.lang.Thread.run(Thread.java:841) 
                                                                   Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                      at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6809)
                                                                      at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1078)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                      at android.view.View.requestLayout(View.java:16775)
                                                                    at a

Upvotes: 0

Views: 201

Answers (1)

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

The operator delay subscribes directly and emits items with a delay; this will lead to problems if there are a lot of items.

The delaySubscribe operator instead delays the actual subscription operation, which is what you probably want.

Final observation: the stack trace is incomplete, and the most important parts are in the CompositeException; it's usually useful to add these too in the post.

Upvotes: 1

Related Questions