2016-11-10 43 views
0

我有一个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接收事件。为什么?

+0

为什么你需要一个主题?你可以订阅你的服务返回的'Observable'。 –

回答

1

这是因为您没有使用.create()工厂方法创建Subject,并且onSubscribe将在您的订阅回调之前调用,所以您将订阅太晚而错过了该元素。如果您订阅,您可以使用BahaviourSubject,它将重播最后一个元素。

请问您能告诉我们您想达到什么目的,因为我认为您可以以更好的方式撰写观察报告,而不是订阅和发布onNext到主题。

请看看我的例子。我使用RxJava2作为环境。

public class ViewModelTest { 
    class ActionsEvent { 
    } 

    class ActionFetchedEvent extends ActionsEvent { 
     public ActionFetchedEvent(ActionsEvent actionEvent) { 

     } 
    } 

    interface Service { 
     public Observable<ActionsEvent> getActions(); 
    } 

    class MyViewModel { 
     private BehaviorSubject<ActionsEvent> binder; 

     private Service service; 

     public MyViewModel(Service service) { 
      this.service = service; 
      this.binder = BehaviorSubject.create(); 
     } 

     public Observable<ActionsEvent> getBinder() { 
      return binder.doOnSubscribe(disposable -> { 
       service.getActions().subscribe(action -> { 
          binder.onNext(new ActionFetchedEvent(action)); 
         } 
       ); 
      }); 
     } 
    } 

    @Test 
    public void name() throws Exception { 
     Service mock = mock(Service.class); 

     MyViewModel viewModel = new MyViewModel(mock); 

     when(mock.getActions()).thenAnswer(invocation -> { 
      return Observable.just(new ActionsEvent()); 
     }); 

     TestObserver<ActionsEvent> test = viewModel.getBinder().test(); 

     test.assertValueCount(1); 
    } 
} 
+0

谢谢。我已经尝试过它,但它有效,但是当我有2个事件时,用户只收到最后一个。我应该使用ReplaySubject吗?我只想为从服务(http请求)中检索的每个操作发出一个事件。订阅者在UI中绘制每个动作(包含在事件中)。 –

+0

请更新您的问题,并详细说明getActions的功能以及您要实现的功能。我无法正确理解正在发生的事情。另请提供有关getActions将发送多少项目以及getBinder应接收哪些订阅者的信息。 –

+0

已编辑。我希望你能理解并感谢你的时间。 –