Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strong reference to observer leads to signal not getting interrupted #820

Closed
dnadoba opened this issue Mar 15, 2021 · 1 comment
Closed

Comments

@dnadoba
Copy link

dnadoba commented Mar 15, 2021

Hey,

I think I found a bug, but before I dig deeper I want to make sure I'm not overlooking something obvious or if this may be even expected behaviour.
Given the following code I would expect that disposing the signal, an interrupted event would be fired:

func testSignalLifetime() {
    let signalProducerLifetimeEnded = expectation(description: "signal producer lifetime ended")
    var observerStrongRef: Signal<Void, Never>.Observer?
    let signalProducer = SignalProducer<Void, Never> { observer, lifetime in
        observerStrongRef = observer
        lifetime.observeEnded {
            signalProducerLifetimeEnded.fulfill()
        }
    }
    let signalInterrupted = self.expectation(description: "signal interrupted")
    let disposable = signalProducer.startWithSignal { signal, _ in
        signal.observeInterrupted {
            signalInterrupted.fulfill()
        }
    }
    disposable?.dispose()
    wait(for: [signalProducerLifetimeEnded, signalInterrupted], timeout: 0.1)
}

This works as long as we are not creating a strong reference to the observer like observerStrongRef does.
If we keep a strong reference to observer, signalProducerLifetimeEnded is fulfilled but signalInterrupted is not.
After removing observerStrongRef, signalInterrupted will fulfill and the test passes.

Is this a bug?
I'm using ReactiveSwift 6.6.0

Thanks
David

@dnadoba
Copy link
Author

dnadoba commented Mar 16, 2021

Okay I think I got it now.
Without a strong reference to the observer, the observer is deinitialised almost immediately which will fire an interrupted event.
Disposing a signal will not fire an interrupted event.
I thought that disposing a signal will also fire an interrupted event.

nevertheless, I have a follow up question: #821

@dnadoba dnadoba closed this as completed Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant