Search code examples
iosswiftreactive-cocoareactive-cocoa-4

ReactiveCocoa 4: Observing an Action's completed event


I'm observing a reactive cocoa 4 action, so I can run some code when the action is executed.

Here's my action's defintion:

buttonAction = Action<Bool, Void, NoError>() { value in

    return SignalProducer<Void, NoError> { observer, _ in
        dataProvider.addNewTestProduct()

        observer.sendNext()
        observer.sendCompleted()
    }

Here's how I'm doing the observing:

vm.buttonAction.events.observeCompleted {
    print("observed completed")
}
vm.buttonAction.events.observeNext {
    print("observed next")
}

The observation of Next events works correctly. When the action is triggered by a UIButton, the print statement is executed.

However, my problem is that for some reason the observation of the completed event is never triggered. Could this be a bug in ReactiveCocoa or am I doing something incorrect?

Thanks!


Solution

  • I've asked the same question on the ReactiveCocoa github page and got an answer (from @ikesyo). For completeness, I'm providing the answer here:

    This is intended behavior. Since the signature is public let events: Signal<Event<Output, Error>, NoError>, you can observe the inner producer's Completed events as follows:

    vm.buttonAction.events.observeNext { event in
        switch event {
        case let .Next(value): ... // A Next event from the inner producer
        case .Completed: ... // A Completed event from the inner producer
        default: break
        }
    }
    

    Source: https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2784