How do I observe a signal and immediately receive a `next` event if it has already occured?

风格不统一 提交于 2019-12-10 22:57:22

问题


I'm trying to wrap an API call that initializes an object after a network request. I don't want the network request to happen for every new observer, so as I understand it, I shouldn't be using SignalProducer. However, by using a single Signal, only the first usage of it will receive a next event, while any newer subscribers will never receive the current value. How should I be doing this? I'm probably doing something fundamentally wrong with RAC.

extension SparkDevice {
    static func createMainDeviceSignal() -> Signal<SparkDevice, NSError> {
        return Signal {
            sink in
            SparkCloud.sharedInstance().getDevices { (sparkDevices: [AnyObject]!, error: NSError!) -> Void in
                if let error = error {
                    sink.sendFailed(error)
                }
                else {
                    if let devices = sparkDevices as? [SparkDevice] {
                        if devices.count > 0 {
                            sink.sendNext(devices[0])
                        }
                    }
                }
            }
            return nil
        }
    }
}

class DeviceAccess {
    let deviceSignal: Signal<SparkDevice, NSError>

    init() {
        self.deviceSignal = SparkDevice.createMainDeviceSignal()
    }
 }

I considered using MutableProperty, but that seems to require a default property, which doesn't seem to make sense for this.

How should I actually be going about this?


回答1:


What you need is multicasting. However, ReactiveCocoa 3/4 does not offer a simple way to do that (as opposed to Rx) because they often lead to a ton of complexity.

Sometimes it is really necessary, as in your example, and it can be easily implemented using a PropertyType.

I'd start by creating the cold signal that makes the request. That has to be a SignalProducer:

private func createMainDeviceSignalProducer() -> SignalProducer<SparkDevice, NSError> {
    return SignalProducer { observer, _ in
        ....
    }
}

If you were to expose this as is, the side effects would occur every time this producer is started. To multicast these values you can wrap it in a property and expose the property's producer instead:

public final class DeviceAccess {
    public let deviceProducer: SignalProducer<SparkDevice, NoError>
    private let deviceProperty: AnyProperty<SparkDevice?>

    init() {
        self.deviceProperty = AnyProperty(
           initialValue: nil, // we can use `nil` to mean "value isn't ready yet"         
           producer: SparkDevice.createMainDeviceSignal()
                        .map(Optional.init) // we need to wrap values into `Optional` because that's the type of the property
                        .flatMapError { error in 
                              fatalError("Error... \(error)") // you'd probably want better error handling

                              return .empty // ignoring errors, but you probably want something better.
                        }
        )

        self.deviceProducer = deviceProperty
              .producer    // get the property producer
              .ignoreNil() // ignore the initial value
    }
 }

Now DeviceAccess.deviceProducer would replay the values emitted by the underlying producer instead of repeating side-effects. Note however that it's not lazy: the underlying SignalProducer would be started right away.



来源:https://stackoverflow.com/questions/33946571/how-do-i-observe-a-signal-and-immediately-receive-a-next-event-if-it-has-alrea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!