I'm new with Swift, and that's why I'm new with Reactive Cocoa v5 or Reactive Swift.
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];
}];
}];
}
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.
I'm a little bit confused about all this stuff in Reactive Swift, like SignalProducers etc.
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
The important thing to understand about
SignalandSignalProduceris the distinction betweenHotandColdSignals.Basically, a
HotSignal 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).In my experience, real
HotSignals are rare in practice..In contrast, a
ColdSignal is one that cares about its Observers - each subscription to aColdSignal 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.In RAC,
ColdSignals are represented bySignalProducer. You might also think of aSignalProduceras a Factory for Signals (hence the name) -starting aSignalProducerperforms the side effect and returns aSignalon which the events are sent.Thats pretty much what your snippet does.
Disposablehas not changed much since RAC 2.x, you can still use that. You probably have just missed how to use it when creating aSignalProducer:Here's a quick example how to use this can be used: