我有一个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'。 –