为什么SignalProducer不返回信号?

时间:2021-02-04 19:57:55

I feel like I understand all of the basic components of ReactiveCocoa (conceptually), by understanding how to connect all of the pieces together is still a bit confusing.

我觉得我理解ReactiveCocoa的所有基本组件(从概念上讲),通过了解如何将所有组件连接在一起仍然有点令人困惑。

For example, after reading about Signal, I fully expected SignalProducer to just have one start() method which returned a Signal, which you would use like so:

例如,在阅读了有关Signal之后,我完全希望SignalProducer只有一个start()方法返回一个Signal,你可以这样使用:

mySignalProducer.start().observe(myObserver)

Instead, you have to pass an observer into start(), and SignalProducer calls observe() for you:

相反,你必须将观察者传递给start(),SignalProducer为你调用observe():

mySignalProducer.start(myObserver)

This means that the interface of SignalProducer is much larger (more to understand), because all of the variations on observe() have to be duplicated on start() (e.g. startNext(), etc).

这意味着SignalProducer的接口要大得多(更需要理解),因为observe()的所有变量都必须在start()上重复(例如startNext()等)。

I think there are two possibilities here:

我认为这里有两种可能性:

  1. There are technical reasons why start() can't simply return a Signal
  2. 有技术原因,为什么start()不能简单地返回一个信号

  3. I misunderstand the SignalProducer conceptually, leading to wonky expectations of its interface
  4. 我从概念上误解了SignalProducer,导致对其界面的不满

If 1 is the case, I'm guessing that has something to do with memory management and disposables which I don't fully understand yet.

如果是1,我猜这与内存管理和一次性用品有关,我还不完全了解。

I'm more worried that 2 is the case. Internally, my understanding of SignalProducer is basically mapped to the concept of a Factory, e.g.:

我更担心2是这样的。在内部,我对SignalProducer的理解基本上映射到了Factory的概念,例如:

mySignalFactory.createSignal().observe(myObserver)

which is why I'm surprised we don't find a start() which returns a Signal.

这就是为什么我很惊讶我们没有找到一个返回Signal的start()。

I would be hugely appreciative if the community could shed some light here.

如果社区可以在这里阐明一点,我将非常感激。

Thanks!

1 个解决方案

#1


5  

I think the main reason is some events can be sent immediately when the producer starts.

我认为主要原因是生产者开始时可以立即发送一些事件。

For example, if you don't like the start series interface, and want to get a signal directly when start:

例如,如果您不喜欢启动系列界面,并希望在启动时直接获取信号:

extension SignalProducer {
    func getSignalFromStart() -> Signal<Value, Error> {
        var signal: Signal<Value, Error>!
        startWithSignal{ innerSignal, _ in
            signal = innerSignal
        }
        return signal
    }
}

Then you can miss some events. Try this:

然后你可以错过一些活动。试试这个:

// When property.producer starts, it will send its current value immediately
let property = MutableProperty(1)

property.producer.getSignalFromStart().observeValues { value in
    print("getSignalFromStart \(value)") // maybe not what you want, only gets 2
}

property.producer.startWithValues { value in
    print("normal start \(value)") // this normally gets 1 and 2
}

property.value = 2

#1


5  

I think the main reason is some events can be sent immediately when the producer starts.

我认为主要原因是生产者开始时可以立即发送一些事件。

For example, if you don't like the start series interface, and want to get a signal directly when start:

例如,如果您不喜欢启动系列界面,并希望在启动时直接获取信号:

extension SignalProducer {
    func getSignalFromStart() -> Signal<Value, Error> {
        var signal: Signal<Value, Error>!
        startWithSignal{ innerSignal, _ in
            signal = innerSignal
        }
        return signal
    }
}

Then you can miss some events. Try this:

然后你可以错过一些活动。试试这个:

// When property.producer starts, it will send its current value immediately
let property = MutableProperty(1)

property.producer.getSignalFromStart().observeValues { value in
    print("getSignalFromStart \(value)") // maybe not what you want, only gets 2
}

property.producer.startWithValues { value in
    print("normal start \(value)") // this normally gets 1 and 2
}

property.value = 2