Rxswift: if:then:else operator: missing?

Created on 13 Aug 2015  路  7Comments  路  Source: ReactiveX/RxSwift

Hey,

Is there a RxSwift analog for the ReactiveCocoa if:then:else operator?

Returns a signal which passes through nexts and errors from trueSignal and/or falseSignal, and sends completed when both boolSignal and the last switched signal complete

http://cocoadocs.org/docsets/ReactiveCocoa/2.3.1/Classes/RACSignal.html#//api/name/if:then:else:

It seems some other reactiveX implementations have an ifThen operator, but I can't find it here.

If not, please could someone point me to a way to do the following:

-I have a signal 'A' which I want to only pass values if another signal 'B' currently evaluates to 'true' somehow. takeWhile etc complete once the predicate matches true, but I don't want this - I want to have fine-grained control over signal A via signal B. filter doesn't feel right here, because it would need to reach outside its own context to some external state, which wouldn't be very functional :)

Thanks for all advice in advance!
Ian

question

Most helpful comment

@iandundas I think you can also use combineLatest

combineLatest(ifSequence, thenSequence, elseSequence) { condition, thenElement, elseElement in
       if (condition) {
            return thenElement 
      }
      else {
            return elseElement
      }
}

If you really really need if:then:else, then you can just create your own operator that wraps combineLatest.

In that way you an create if:then:elseif:elseif ....:else or what ever imperative operator do you need applied easily :)

Hope this helps.

All 7 comments

If I understand you correctly then something like what I have on line 57 of this file should do it: https://github.com/dtartaglia/MVVMSwiftSample/blob/feature/make_reactive/MVVM/DetailViewController.swift

In short I think sampleLatest sounds like what you need. You might have to combine the parameter of sampleLatest with a filter.

@iandundas I think you can also use combineLatest

combineLatest(ifSequence, thenSequence, elseSequence) { condition, thenElement, elseElement in
       if (condition) {
            return thenElement 
      }
      else {
            return elseElement
      }
}

If you really really need if:then:else, then you can just create your own operator that wraps combineLatest.

In that way you an create if:then:elseif:elseif ....:else or what ever imperative operator do you need applied easily :)

Hope this helps.

_problem magically rearranges in head, combineLatest solution condenses into view_
... okay that actually could work! Thanks both, I'll post back if still stuck. 馃憤馃徎

So, (if signal A's events are allowed through only if signal B evaluates to true, described above), what would you return if you didn't want anything to be passed to subscribers of combineLatest? i.e. I'm looking for an empty signal that just gets dropped, somehow.

i.e.:


let automaticUpdatesShouldBeEnabled: Observable<Bool>
let annotations: Observable<[PointAnnotation]>

combineLatest(automaticUpdatesShouldBeEnabled, annotations, {
    enabled, annotations in

    if enabled{ // cool, let the annotations through
        return annotations 
    }
    else{ // we want to stop here, and subscribers should receive nothing.
        // return ?? 
    }

})

I guess one way would be to just make annotations an optional (Observable<[PointAnnotation]?>) and then just return nil - and then add a filter afterwards to filter these out.

But is there a better way?

Hi @iandundas

I think what you need is a combo of combineLatest and flatMap. Something like:

let automaticUpdatesShouldBeEnabled: Observable<Bool>
let annotations: Observable<[PointAnnotation]>

combineLatest(automaticUpdatesShouldBeEnabled, annotations, { ($0, $1) })
    >- flatMap { enabled, annotations in
         if enabled{ // cool, let the annotations through
            return just(annotations) 
        }
        else{ // we want to stop here, and subscribers should receive nothing.
            return empty()
        }
     }

That's perfect for what I need - thanks so much :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RobinFalko picture RobinFalko  路  3Comments

Z-JaDe picture Z-JaDe  路  3Comments

tyregor picture tyregor  路  3Comments

RafaelPlantard picture RafaelPlantard  路  3Comments

trant picture trant  路  3Comments