如何在收到onNext()后自动取消订阅?
现在我用这个代码:
rxObservable
.compose(bindToLifecycle()) // unsubscribe automatically in onPause() if method was called in onResume()
.subscribe(new Subscriber<Object>() {
...
@Override
public void onNext(Object o) {
unsubscribe();
}
});
Run Code Online (Sandbox Code Playgroud) 在RecyclerView中,我有一个ViewHolder带有VideoView和Button"Like"的状态(选择与否).
我的Presenter有一个方法可以更新模型中的"Like"状态 - VideoEntity.在回调中我需要更新View form Presenter,所以我打电话getView().updateItem(VideoEntity entity).之后,我应该找到recyclerView实体位置,并使项目无效.所以我想避免这种情况.
在经典架构中,我可以调用一些方法ViewHolder,在那里获得一些回调并更新已更改的数据.
如何将此伪代码迁移到MVP模式?
@Override
public void onBindViewHolder(ContainerViewHolder holder, int position) {
...
rx.subscribe(result -> holder.update(result));
}
@Override
public void onViewRecycled(ContainerViewHolder holder) {
if (haveBackgroundRequests) { rx.unsubscribe(holder); }
}
Run Code Online (Sandbox Code Playgroud)