2017-05-08 78 views
0

我使用RxFirebaseDatabase https://github.com/ezhome/Android-RxFirebase。 如何延迟订阅observeValueEvent?我的代码Android RxFirebaseDatabase延迟订阅

Fragment.java

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

RxFirebaseDatabase.java方法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方法

@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; 
    } 
    }; 
    } 

我得到致命异常扔在Scheduler.Worker线程。你能帮我吗 ?

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 
+0

您是否试过'delaySubscription'而不是普通的'delay'? –

+0

延迟到什么时候?什么是抛出异常和在哪里? – yosriz

+0

@yosritz我想推迟所有物品的排放。我发布了logcat。 – eurosecom

回答

1

delay直接订阅和发射具有一延迟的项目的操作员;这会导致问题,如果有很多项目。

delaySubscribe运算符会延迟实际的订阅操作,这可能是您想要的操作。最后的观察:堆栈跟踪不完整,最重要的部分在CompositeException;在帖子中添加这些通常也很有用。

+0

我将添加整个logcat。我需要延迟onNext和delaySubscribe不是直接我需要的。 – eurosecom

+0

我发布了整个logcat。 – eurosecom

+0

这是实际的原因:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程可以触及其视图。 - 发生这种情况是因为'delay'运算符隐式切换到IO调度程序。 –