Factory closure lifetime differs between .create and .deferred, subscriptions to the latter keep its factory closure alive #2533
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Short description of the issue:
Each subscription to
Observable.deferred
captures its factory closure, unlikeObservable.create
.Expected outcome:
Intuitively I would expect
Observable.deferred
to not keep its factory closure alive longer than required in subscriptions, but that's probably not the best argument.Alternatively, I'd expect this behavior documented because I recognize that changing it at this point might be a very breaking change.
What actually happens:
.deferred
is implemented in such a way each subscription to it retains its factory closure:RxSwift/RxSwift/Observables/Deferred.swift
Line 71 in 2ff073f
Self contained code example that reproduces the issue:
See the test cases in this PR.
testDeferredFactoryClosureLifetime
fails whiletestObservableFactoryClosureLifetime
passes.RxSwift/RxCocoa/RxBlocking/RxTest version/commit
6.6.0
Platform/Environment
How easy is to reproduce? (chances of successful reproduce after running the self contained code)
Xcode version:
Installation method:
I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)
Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)