Add new subscribe/drive/emit with object, Deprecate withUnretained on Driver #2290
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.
tl;dr
This PR will:
Why are we doing this?
RxSwift 6.0 added a new
withUnretained
operator that creates a weak reference and then retains it within the next emission, so you could doobservable.withUnretained(self)
and getself
with the element in parity.Unfortunately there is a small side-effect we didn't consider. If the stream you're working with has
share(replay: 1)
, the replay buffer would bufferself
along with the value, which might create a retain cycle in some scenarios.This in itself isn't an issue (just a documentation change), but for
Driver
specifically, the default is to replay the latest value, so we are forced to immediately deprecatedwithUnretained
onDriver
, specifically. It is still available on all other traits,Observable
andSignal
.This is a very rare decision, to deprecate something in a patch release, but given this is a very new API, we prefer to deprecate it ASAP, before any widespread adoption.
Along with this deprecate, we're adding a new set of APIs to have a similar action for different pieces. For
Driver
, this looks like this:This API is more concise and is available for all pieces in RxSwift and RxCocoa: Observable, Infallible, Driver, Signal, Completable, Single, and Maybe.