2017-05-08 75 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調度程序。 –