从 RACSignal 迁移到 ReactiveSwift 或 RAC5 [英] Migrate from RACSignal to ReactiveSwift or RAC5

查看:55
本文介绍了从 RACSignal 迁移到 ReactiveSwift 或 RAC5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 的新手,这就是为什么我是 Reactive Cocoa v5 或 Reactive Swift 的新手.

I'm new with Swift, and that's why I'm new with Reactive Cocoa v5 or Reactive Swift.

以前我在 RAC 2.x 中使用 RACSignal,我喜欢做这样的事情:

Previously I used RACSignal with RAC 2.x and I liked to do something like this:

- (RACSignal *)signalForGET:(NSString *)URLString parameters:(NSDictionary *)parameters {
  return [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
      AFHTTPRequestOperation *op = [self GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
          [subscriber sendNext:responseObject];
          [subscriber sendCompleted];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          [subscriber sendError:error];
      }];
      return [RACDisposable disposableWithBlock:^{
          [op cancel];
      }];
   }];
}

在这里我喜欢它取消一次性请求,我也可以通过在返回的信号上调用 dispose 方法来手动取消它.

And here I loved that it cancels request on disposable, and also I could cancel it manually by calling dispose method on the returned signal.

我对 Reactive Swift 中的所有这些东西都有些困惑,比如 SignalProducers 等.

I'm a little bit confused about all this stuff in Reactive Swift, like SignalProducers etc.

请举例说明如何使用最新的 Swift/ReactiveSwift/ReactiveCocoa 版本实现相同的功能.主要要求是能够在任何我想要的地方取消请求(或处理信号),并在处理时自动取消请求

Please give me example how to implement the same with newest Swift/ReactiveSwift/ReactiveCocoa versions. Main requirement is to have ability to cancel request (or dispose signal) wherever I want, and to have request automatically getting cancelled on dispose

推荐答案

了解SignalSignalProducer 的重要一点是Hot 之间的区别code> 和 Cold 信号.

The important thing to understand about Signal and SignalProducer is the distinction between Hot and Cold Signals.

基本上,Hot 信号是一种不关心其观察者的信号.它发送它的值,无论它是有一个、多个还是根本没有观察者.最重要的是:新观察不会对信号产生副作用,每个新订阅者将获得与其他订阅者完全相同的事件(减去订阅之前已经发生的事件!)!考虑诸如用户输入、传感器数据……(忽略启动/停止传感器之类的事情).

Basically, a Hot Signal is one that does not care about its Observers. It sends its values, no matter if it has one, multiple or even no observer at all. And most important: New observations do not cause side effects in the signal and each new subscriber will get the exact same events as the other subscribers (minus the ones that have already occurred before the subscription!)! Think things like user input, sensor data, ... (ignoring things like starting/stopping the sensor).

根据我的经验,真正的 Hot 信号在实践中很少见..

In my experience, real Hot Signals are rare in practice..

相比之下,Cold 信号是一个关心其观察者的信号 - 对 Cold 信号的每次订阅都可能产生副作用,订阅者会根据该信号接收事件副作用.因此,两个不同的观察者各自启动副作用一次并获得不同的事件集.

In contrast, a Cold Signal is one that cares about its Observers - each subscription to a Cold Signal potentially performs a side effect and the subscriber receives events based on that side effect. So two different observers each start the side effect once and get distinct sets of events.

在RAC中,Cold信号由SignalProducer表示.您可能还会将 SignalProducer 视为信号工厂(因此得名) - start 使用 SignalProducer 执行副作用并返回一个Signal 发送事件的信号.

In RAC, Cold Signals are represented by SignalProducer. You might also think of a SignalProducer as a Factory for Signals (hence the name) - starting a SignalProducer performs the side effect and returns a Signal on which the events are sent.

这几乎就是您的代码段所做的.

Thats pretty much what your snippet does.

Disposable 自 RAC 2.x 以来没有太大变化,您仍然可以使用它.在创建 SignalProducer 时,您可能刚刚错过了如何使用它:

Disposable has not changed much since RAC 2.x, you can still use that. You probably have just missed how to use it when creating a SignalProducer:

func producerForGET(urlString: String, parameters: [String: String]) -> SignalProducer<Data, NSError> {
    return SignalProducer<Data, NSError> { observer, disposable in
        let operation = GET(url: urlString, parameters: parameters, success: { operation, responseObject in
            observer.send(value: responseObject)
            observer.sendCompleted()
        }, failure: { error in
            observer.send(error: error)
        })

        disposable += {
            print("Disposed")
            operation.cancel()
        }
    }
}

这是一个如何使用它的快速示例:

Here's a quick example how to use this can be used:

producerForGET(urlString: "Bla", parameters: [:])
    .start()    // Performs the request and returns a Signal
    .dispose()  // Runs the disposable block and cancels the operation

这篇关于从 RACSignal 迁移到 ReactiveSwift 或 RAC5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆