Reputation: 1017
I am using ReactiveCocoa 4.0 with Swift. I have a signal called startedSignal
of type Signal<String, NoError>
that I subscribe to in a view controller.
startedSignal.observeNext { _ in
// Do stuff
}
I basically want to wait a number of seconds, then do something else if startedSignal
doesn't send in any next values. I looked at the docs and saw things like retry
on SignalProducer
but I am not sure how that can be used to achieve this, given that startedSignal
doesn't complete or send errors.
Upvotes: 0
Views: 1240
Reputation: 1743
While Rex
is useful if you have some more advanced use cases and you don't want to implement this logic yourself, you can actually do this with the existing operators in ReactiveCocoa
, using a combination of timeoutWithError
and flatMapError
or retry
:
signal
.promoteErrors(Error.self)
.timeoutWithError(
.Timeout,
afterInterval: interval,
onScheduler: QueueScheduler()
)
.flatMapError { error in
return anotherProducer
}
// Somewhere else:
private enum Error: ErrorType {
case Timeout
}
Upvotes: 5
Reputation: 25917
I think Rex's timeout is what you want. It would look like this:
let alternative: Event<String, NoError> = ...
startedSignal.timeoutAfter(1.0, event: alternative, onScheduler: UIScheduler()).observeNext { _ in
// Do stuff
}
Upvotes: 3