ios - 没有订阅者时停止发布,有订阅者时自动开始

标签 ios objective-c cocoa-touch reactive-programming reactive-cocoa

我如何实现一个 RACSignal,它会在没有订阅者时停止发布并在有订阅者时自动启动?

这是一个场景:

假设我在 AppDelegate 中有一个 currentLocationSignal。 我的 LocationViewController 将在 View 加载时订阅 currentLocationSignal 并在 View 卸载时取消订阅(处置)。由于获取当前位置需要几秒钟,我希望在应用程序打开时始终订阅 currentLocationSignal(并在几秒钟后自动取消订阅),所以当我到达 LocationViewController 我会得到一个准确的位置。 因此信号的订阅者可能不止一个。当第一个订阅者监听时,它需要开始调用 startUpdatingLocation,当没有订阅者时它需要调用 stopUpdatingLocation

最佳答案

好问题!通常,您会使用 RACMulticastConnection对于这样的用例,但是,因为您希望信号能够在以后重新激活,所以连接本身并不合适。

最简单的答案可能是模仿连接的工作方式,但具有您想要的特定行为。基本上,我们会跟踪有多少 subscribers在任何给定时间都有,并根据该数字开始/停止更新位置。

让我们从添加一个 locationSubject 属性开始。主题需要是 RACReplaySubject ,因为我们总是希望新订阅者立即获得最近发送的位置。实现该主题的更新非常容易:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self.locationSubject sendNext:locations.lastObject];
}

然后,我们要实现跟踪和递增/递减订阅者数量的信号。这通过使用 numberOfLocationSubscribers 整数属性来实现:

- (RACSignal *)currentLocationSignal {
    return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
        @synchronized (self) {
            if (self.numberOfLocationSubscribers == 0) {
                [self.locationManager startUpdatingLocation];
            }

            ++self.numberOfLocationSubscribers;
        }

        [self.locationSubject subscribe:subscriber];

        return [RACDisposable disposableWithBlock:^{
            @synchronized (self) {
                --self.numberOfLocationSubscribers;
                if (self.numberOfLocationSubscribers == 0) {
                    [self.locationManager stopUpdatingLocation];
                }
            }
        }];
    }];
}

在上面的代码中,+createSignal: block 在每次将新订阅者添加到返回的信号时被调用。发生这种情况时:

  1. 我们检查订阅者数量当前是否为零。如果是,则刚添加的订阅者是第一个,因此我们需要启用(或重新启用)位置更新。
  2. 我们将订阅者直接连接到我们的 locationSubject,因此来自后者的值会自动馈送到前者。
  3. 然后,在未来的某个时间,当订阅为 disposed of 时,我们会减少计数并在适当时停止位置更新。

现在,剩下的就是在启动时订阅 currentLocationSignal,并在几秒钟后自动取消订阅:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Use a capacity of 1 because we only ever care about the latest
    // location.
    self.locationSubject = [RACReplaySubject replaySubjectWithCapacity:1];

    [[self.currentLocationSignal
        takeUntil:[RACSignal interval:3]]
        subscribeCompleted:^{
            // We don't actually need to do anything here, but we need
            // a subscription to keep the location updating going for the
            // time specified.
        }];

    return YES;
}

这会立即订阅 self.currentLocationSignal,然后在 +interval: 信号发送它的第一个值时自动处理该订阅。

有趣的是,-[RACMulticastConnection autoconnect] 曾经表现得像上面的-currentLocationSignal,但是that behavior was changed因为它使副作用变得难以预测。这个用例应该是安全的,但在其他时候(比如发出网络请求或运行 shell 命令时)自动重新连接会很糟糕。

关于ios - 没有订阅者时停止发布,有订阅者时自动开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14769185/

相关文章:

ios - 数组索引超出范围

objective-c - 将工作委托(delegate)给两个不同的委托(delegate),这可能吗?

ios - 如何将 UITableView 的 header 设置为属性字符串?

ios - 歌曲更改时如何更新 MPMusicPlayerController

ios - 延迟次级子 UIViewController 预加载以实现超快速应用程序加载的最佳方法?

ios - 如何确定 DDXMLElement 是否有值?

objective-c - 如何使用 Cocoa 监控文件夹?

iphone - 有谁知道 iphone 地址簿框架的包装器?

Objective-C 模型- View 通信

ios - UIView 的动画帧变化不平滑,导致有弹性的效果