0

The following func runs well before I update to Xcode12, I'm new to ReactiveSwift and I don't know how to fix this issue, thanks for you help!

enter image description here

Error message: Cannot convert value of type 'Disposable?' to closure result type 'Void'

 public func testFunc(input: Signal<Value, Never>.Event = .completed) -> Signal<Value, Never> {

    return Signal<Value, Never> { observer, lifetime in
        return self.signal.observe { event in
            switch event {
            case .value(let value):
                observer.send(value: value)
            case .failed(_):
                observer.send(input)
            }
        }
    }
}
ImWH
  • 820
  • 1
  • 7
  • 20
  • Check your return type, initializer requires `(Observer, Lifetime) -> Void` bug you give it `(Observer, Lifetime) -> Disposable?` – Zhang Nov 17 '20 at 10:02

1 Answers1

0

This was a change made in newer ReactiveSwift versions. Instead of returning a disposable, you should add it to the lifetime passed to the closure:

public func testFunc(input: Signal<Value, Never>.Event = .completed) -> Signal<Value, Never> {

    return Signal<Value, Never> { observer, lifetime in
        lifetime += self.signal.observe { event in
            switch event {
            case .value(let value):
                observer.send(value: value)
            case .failed(_):
                observer.send(input)
            }
        }
    }
}

It seems like there are also some missing cases in that switch statement though.

jjoelson
  • 5,771
  • 5
  • 31
  • 51
  • Thanks for your answer! Using your code I try to delete `lifetime +=` It's also no error in it, Does it make a difference? (I delete some case for better readability, It's ok) – ImWH Nov 18 '20 at 05:12
  • In your closure, you're observing `this.signal`. If you want that observation to be cancelled when the current signal finishes, then you need to use `lifetime +=`. Otherwise the observation will continue indefinitely. – jjoelson Nov 18 '20 at 14:09