我有一個ViewModel
類,它暴露了PublishSubject
活頁夾。PublishSubject訂閱者未收到活動
視圖模型
public class ViewModel {
private PublishSubject<ActionsEvent> binder = PublishSubject.create();
private Service service = createService();
@Override
public Observable<ActionsEvent> getBinder() {
return binder.doOnSubscribe(initialize());
}
private Action0 initialize() {
return new Action0() {
@Override
public void call() {
service.getActions().subscribe(new Action1<Action>() {
@Override
public void call(Action action) {
Log.d(TAG, "So far, so good");
binder.onNext(new ActionFetchedEvent(action));
}
});
}
};
}
}
而在活動,它訂閱動作時每個事件被取出被執行。
活動
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstance) {
//More code
viewModel.getBinder().subscribe(new Action1<ActionsEvent>() {
@Override
public void call(ActionsEvent event) {
Log.d(TAG, "This is not printed!!");
paintActionInUserInterface(event.getAction());
}
});
}
}
服務
public interface ActionsService {
@GET("/actions")
Observable<Action> getActions(); //Performs an HTTP request with Retrofit
}
ActionFetchedEvent
public class ActionFetchedEvent implements ActionsEvent {
private Action action;
//getters and setters
}
但沒有按訂戶」 t接收事件。爲什麼?
爲什麼你需要一個主題?你可以訂閱你的服務返回的'Observable'。 –