I used to use ReactiveCocoa in Objective-C but I've since switched to RxSwift as I found it easier to understand than RAC4. However there's something I used to do in RAC that was useful:
@weakify(self);
[[RACCommand alloc] initWithEnabled:RACObserve(self, valid) signalBlock:^RACSignal *(id input) {
@strongify(self);
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//make network call
//send responseObject to subscriber
[subscriber sendNext:responseObject];
[subscriber sendCompleted];
return nil;
}] materialize];
}];
This allowed me to subscribe to the command for it's executing state as well as its execution signals so that I could observe data that is returned from the call.
I'm not sure how to reproduce this with RxSwift Action. I am only able to subscribe to its executing observable:
var loader: NotificationType?
formButton.rx_action!.executing.subscribeNext({ [weak self] (executing) -> Void in
if executing {
loader = self?.showNotification(.Loading, title: self?.viewModel.loaderTitle.value, message: "Please wait".localized, timeout: -1)
}
else {
if let loader = loader {
loader.dismiss()
}
}
}).addDisposableTo(disposeBag)
But I then have to create an additional PublishSubject
to send my response data:
viewModel.submitSubject.subscribe(onNext: { (response) -> Void in
print(response)
}, onError: { (error) -> Void in
print(error)
}, onCompleted: { () -> Void in
//completed
}) { () -> Void in
}.addDisposableTo(disposeBag)
Is there a way to create a similar pattern in RxSwift with Action?