我有一个相当复杂的功能,我想被动地实现.是否启用此功能取决于以下内容
I have a fairly complex feature I want to implement reactively. Whether this feature is enabled depends on the following
以下是我已有的可观察对象.
Here are the following observables I already have.
Observable - LifeCycleEvent 是一个与 Android 活动生命周期相关的枚举.
Observable<LifeCycleEvent> - LifeCycleEvent is an enum that relates to Android's activity lifecycle.
Observable 和 Observable 订阅后,启动一个流程密集型任务以启用我的功能.这两个 observable 需要成为
Observable<Void> and Observable<Void>, when subscribed to, kick off a process intensive task to enable my feature.. These two observables need to be
- 订阅当 #2 发出 LifeCycleEvent#isStarted() 返回 true 和 #1 发出 true 的事件(用户启用的功能)
- 取消订阅当 #2 发出 LifeCycleEvent#isStarted() 返回 false 的事件时
- 在#2 发出LifeCycleEvent#isDestoryed() 返回true 的事件时取消订阅.我还需要在这里取消订阅 #1 和 #2,因为这意味着该类已准备好进行垃圾收集.
- 从 #1 发出 false 时取消订阅(用户未启用此功能)
- subscribed to when #2 emits an event that LifeCycleEvent#isStarted() returns true and #1 emits true (user enabled feature)
- unsubscribed to when #2 emits an event that LifeCycleEvent#isStarted() returns false
- unsubscribed to when #2 emits an event that LifeCycleEvent#isDestoryed() returns true. I will also need to unsubscribe from #1 and #2 here because that means this class is ready for garbage collection.
- unsubscribed from when #1 emits false (user did not enabled this feature)
我怎样才能最好地做到这一点?我有一个类将采用上述依赖项和一个名为 void startObserving() 的方法,它将启动观察过程(并在此实例的生命周期内调用一次.我想我需要以某种方式维护两个订阅(一个用于 #1 和 #2,另一个用于 #3).
How can I best do this? I have a single class that will take the above mentioned dependencies and a single method called void startObserving() that will kick the observation process off (and be called once for the lifetime of this instance. I think I will need to somehow maintain two subscriptions (one for #1 & #2, and another for #3).
推荐答案哦,这是一个难题,我认为:
Oooh, this is a puzzler, I think:
Observable<Boolean> featureEnabled = ...; Observable<LivecycleEvent> lifecycle = ... .filter(event -> event.isStarted() !=null || event.isDestroyed()); Observable<Void> processA = ...; Observable<Void> processB = ...; Observable<Void> processes = Observable.merge(processA, processB); ObservablebineLatest(featureEnabled, lifecycle, (feature, event) -> Pair.of(feature && event.isStarted(), event.isDestroyed())) .takeUntil(pair -> pair.getRight()) .map(Pair::getLeft) .debounceUntilChanged() .switchMap(flag -> flag ? processes : Observable.empty()) .subscribe(....)更多推荐
如何创建一个可以取消订阅和重新订阅的可观察序列?
发布评论