screenMonkey MonkeyMan
screenMonkey MonkeyMan

Reputation: 423

How do I get from Observable<BleHandler.BlePeripheral> to BleHandler.BlePeripheral?

I have a variable that gets me the type Observable<BleHandler.BlePeripheral> after using flatMap on the array.

let scannedPeripheral: Observable<BleHandler.BlePeripheral> = instance.bleScan()
            .flatMap{ Observable.from($0)}

But now I need to use that variable in another function that takes BleHandler.BlePeripheral:

instance.bleEstablishConnection(scannedPeripheral: scannedPeripheral)

Obviously it doesn't work. Is there a way to get my Observable<BleHandler.BlePeripheral> to just BleHandler.BlePeripheral

Upvotes: 1

Views: 62

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

It depends on whether or not the function returns a value and what type of value it returns...

If the function is void and you are just calling it for side effects then:

let disposable = scannedPeripheral
    .subscribe(onNext: { instance.bleEstablishConnection(scannedPeripheral: $0) })

If your function has side effects and returns an Observable then:

let returnValue = scannedPeripheral
    .flatMap { instance.bleEstablishConnection(scannedPeripheral: $0) }

If the function has no side effects and you are just calling it to transform your value into a different value then:

let returnValue = scannedPeripheral
    .map { instance.bleEstablishConnection(scannedPeripheral: $0) }

This last one is unlikely based on the name of the function, but I put it here for completeness.

Upvotes: 1

Related Questions