2016-07-25 1 views
1

私はPublishedSubjectに登録済みのdoOnSubscribedoOnUnsubscribeアクションを持っています。サブスクリプションが行われている場合、両方のアクションは呼び出されません。PublishSubject doOnSubscribeが呼び出されていません

private PublishSubject<Long> publishSubject; 
private Subscription subscription; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);  
    publishSubject = PublishSubject.create(); 
    publishSubject.doOnSubscribe(new Action0() { 
     @Override 
     public void call() { 
      Log.d("SUBJECT", "Someone subscribed."); 
     } 
    }); 
    publishSubject.doOnUnsubscribe(new Action0() { 
     @Override 
     public void call() { 
      Log.d("SUBJECT", "Someone UNsubscribed."); 
     } 
    }); 

    Observable.interval(1, TimeUnit.SECONDS).subscribe(new Action1<Long>() { 
     @Override 
     public void call(final Long tick) { 
      publishSubject.onNext(tick); 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    subscription = publishSubject.subscribe(new Action1<Long>() { 
     @Override 
     public void call(final Long aLong) { 
      Log.d("SUBJECT", "Got tick " + aLong); 
     } 
    }); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    subscription.unsubscribe(); 
} 

しかし、私のlogcatで、私だけ"Got tick "メッセージなし"Someone subscribed"を取得します。サブスクライブがonResume()にあり、退会がonPause()であるとき

07-25 17:57:34.110 8753-8965/com.example.plinzen.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4 
07-25 17:57:34.954 8753-8964/com.example.plinzen.myapplication D/SUBJECT: Got tick 0 
07-25 17:57:35.950 8753-8964/com.example.plinzen.myapplication D/SUBJECT: Got tick 1 
07-25 17:57:36.950 8753-8964/com.example.plinzen.myapplication D/SUBJECT: Got tick 2 
07-25 17:57:37.950 8753-8964/com.example.plinzen.myapplication D/SUBJECT: Got tick 3 
07-25 17:57:38.949 8753-8964/com.example.plinzen.myapplication D/SUBJECT: Got tick 4 
07-25 17:57:39.950 8753-8964/com.example.plinzen.myapplication D/SUBJECT: Got tick 5 

任意のアイデアは、なぜこれらのアクションは、呼び出されませんか? Subjectトピックを誤解していますか?

private Observable<Long> publishedObservable; 

[...] 

PublishSubject<Long> publishSubject = PublishSubject.create(); 
publishedObservable = publishSubject.doOnSubscribe(new Action0() { 
    @Override 
    public void call() { 
     Log.d("SUBJECT", "Someone subscribed."); 
    } 
}).doOnUnsubscribe(new Action0() { 
    @Override 
    public void call() { 
     Log.d("SUBJECT", "Someone UNsubscribed."); 
    } 
}); 

そして、他の方法でpublishedObservableを使用します。

+0

説明*サブスクリプションが行われていれば、どちらのアクションも呼び出されません。* –

答えて

6

は、チェーンを壊さないでください。

あなたのコードでは、2つの新しいObservableを作成しますが、それらを直接スローします。 Observableを変更しないメソッドは、目的の動作を実装する新しいオブジェクトを作成します。

+0

私は愚かな間違いです!ご協力ありがとうございました – Christopher

関連する問題