如何创建一个可以取消订阅和重新订阅的可观察序列?

编程入门 行业动态 更新时间:2024-10-18 01:31:39
本文介绍了如何创建一个可以取消订阅和重新订阅的可观察序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个相当复杂的功能,我想被动地实现.是否启用此功能取决于以下内容

I have a fairly complex feature I want to implement reactively. Whether this feature is enabled depends on the following

  • 已启用此功能的用户首选项(用户可以打开或关闭此功能)
  • 活动已启动 (Activity#onStart()).如果您不熟悉 Android,这仅表示该视图对用户可见.
  • User preference for this feature has been enabled (user can toggle this feature on or off)
  • Activity is started (Activity#onStart()). If you aren't familiar with Android, this is just means the view is visible to the user.
  • 以下是我已有的可观察对象.

    Here are the following observables I already have.

  • Observable - 是否启用此功能的用户首选项.true 如果启用,false 如果禁用.
  • Observable - LifeCycleEvent 是一个与 Android 活动生命周期相关的枚举.

  • Observable<Boolean> - a user preference for whether this feature is enabled. true if enabled, false if disabled.
  • 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(....)
  • 更多推荐

    如何创建一个可以取消订阅和重新订阅的可观察序列?

    本文发布于:2023-11-27 23:30:41,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1640036.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:序列   创建一个

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!