Migrating from RACSignal to ReactiveSwift or RAC5

I am new to Swift, and therefore I am new to Reactive Cocoa v5 or Reactive Swift.

I used to use RACSignal with RAC 2.x, and I liked doing 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 liked that it cancels the one-time request, and I can also cancel it manually by calling the method disposeon the returned signal.

I got a little confused about all this in Reactive Swift like SignalProducer etc.

Please give me an example of how to implement the same with the latest versions of Swift / ReactiveSwift / ReactiveCocoa. The main requirement is to be able to cancel the request (or delete the signal) wherever I want and request automatic receipt of the delete request

+3
1

Signal SignalProducer - Hot Cold.

, Hot - , . , , , . : , , ( , !)! , , ,... ( , / ).

, Hot .

, Cold - , - Cold , . , .

RAC, Cold SignalProducer. SignalProducer Factory ( ) - start ing a SignalProducer Signal, .

, .

Disposable RAC 2.x, . , , 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()
        }
    }
}

:

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

Source: https://habr.com/ru/post/1675647/


All Articles