nowadays I am interested in ReactiveSwift and trying to add it for networking actions of the project. But there are some problems about action return type with observeResult method.
view model:
final class TestViewModel: ViewModel {
var getListAction: Action<Void, Void, APIError>!
var arr = NSMutableArray()
override init() {
super.init()
self.getListBuild()
}
func getListBuild() {
getListAction = Action<Void, Void, APIError> { _ in
let signalProducer = SignalProducer<Void, APIError> { observer, disposable in
self.getList(observer)
}
return signalProducer
}
getListAction.apply().start()
}
func getList(_ observer: Observer<Void, APIError>) {
Network<UserDetailInfo>().requestList(router: RegistrationRouter(endpoint:.Login())).take(during: lifetime).startWithResult { (result) in
switch result {
case let .success(value):
self.arr = value as! NSMutableArray
print("success")
observer.sendCompleted()
case let .failure(error):
print("abort: \(String(describing: error.message))")
observer.send(error: error)
}
}
}
}
and from the controller I have tried to manage it like this :
final class ViewController: View {
var viewModel: TestViewModel?
func bindViewModel() {
self.viewModel = TestViewModel()
self.viewModel?.getListAction.events.take(during: lifetime).observeResult { (result) in
switch result {
case .success(_):
print("success")
print(self.viewModel?.arr.count as Any)
break
case let .failure(error):
print(error.localizedDescription);
break
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.bindViewModel()
}
}
Despite of returning failure from getList method in viewModel, situation like this :
method returned success but it is not. It should be failure. Please let me learn what the problem is about this subject.
The event
stream you observe will never return a failure. See its documentation:
/// A signal of all events generated from applications of the Action.
///
/// In other words, this will send every `Event` from every signal generated
/// by each SignalProducer returned from apply() except `ActionError.disabled`.
public let events: Signal<Event<Output, Error>, NoError>
To observe successful values, you would want to subscribe to the values
and use observeValues
:
self.viewModel?.getListAction
.values
.take(during: lifetime)
.observeValues { [weak self] value in
// always successful
}
If you want to observe errors, you can subscribe to errors
and use observeValues
.
self.viewModel?.getListAction
.errors
.take(during: lifetime)
.observeValues { [weak self] error in
// error event
}