Description
No deprecation warning in Xcode on using Variable:
As per release notes provided here, Variable
is deprecated in the favor of BehaviorRelay
. But I dont see any deprecation warning on using Variable in Xcode
Expected outcome:
Xcode should alert the deprecation of Variable.
What actually happens:
I am using RxSwift4
. My pod specs looks like
pod 'RxSwift', '~> 4.0'
pod 'RxCocoa', '~> 4.0'
My project is making use of Swift4
. When I declare a variable
var emyFilter = Variable<[MyFilterModel]>([MyFilterModel(data: "{:}")])
No warning is shown, realized the deprecation of Variable
only when somebody specified it in their answer on SO
Just realized that I cant even access BehaviorRelay
with RxSwift (4.0.0) in pod repo. Am I missing anything here? Is BehaviorRelay
available in RxSwift4.0 and what should be our course of action? Should we still stick with Variable
or BehaviorRelay
? There are loads of content on web regarding Variable
but dont see much on BehaviorRelay
yet, so little confused of its usage as well. As I cant access it in my Xcode, cant even access the source code
RxSwift/RxCocoa/RxBlocking/RxTest version/commit
Using RxCocoa (4.0.0)
Using RxSwift (4.0.0)
Platform/Environment
- iOSmacOStvOSwatchOSplaygrounds
How easy is to reproduce? (chances of successful reproduce after running the self contained code)
- easy, 100% reprosometimes, 10%-100%hard, 2% - 10%extremely hard, %0 - 2%
Xcode version:
Version 9.1 (9B55)
Installation method:
- CocoaPodsCarthageGit submodules
I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)
- yes (which ones)no
Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)
- just startingI have a small code baseI have a significant code base
Activity
kzaher commentedon Nov 26, 2017
Hi @sandeeplearner ,
Yes, so far we haven't put deprecation warnings on it since that API was pretty heavily used and I wanted to ease the 4.0 migration path.
We are currently preparing it for deprecation, but if somebody sees it there they will be incline not to use it.
Another reason why we haven't add deprecation warnings is because a clear deprecation path isn't decided yet, the only thing that is decided is that that concept will be deprecated in RxSwift target because:
PublishRelay
). It models state only.*Relay
On the other side:
Behavior
naming is novice user unfriendly)The current plan is this:
Deprecated.swift
typealias Variable = BehaviorRelay
and removing deallocation completion.Variable
from RxSwift in future versions.So far we are at:
figure out how to best sanitize the....
I'll probably add this issue to
Variable
comments so this plan is publicly visible and users are better informed.Adds deprecation explanation for Variable. #1501
sandeeplearner commentedon Nov 27, 2017
@kzaher : Thanks a lot 👍
DevAndArtist commentedon Dec 5, 2017
@kzaher one pain point migrating
Variable
toBehaviorRelay
was thatvalue
is read-only, this disallows in-place mutation of the value - especially if the value is some kind of a collection. This requires a workaround like:kzaher commentedon Dec 5, 2017
@DevAndArtist
this is a dangerous patten because after you read
relay.value
a new value could be set before you callrelay.accept(mutableCopy)
and thus you could accidentally overwrite something.I am assuming people were doing this a lot, and because of Swift accessor semantics these issues where hidden, but I would say this is an anti-pattern for the mentioned reasons.
That's why we haven't made
Variable
deprecated yet. Even though I know it's theoretically correct thing to do, people have a lot of faulty existing code that works for them because of some implicit assumptions.For example, if you assume all code is executed on main thread, then this can't happen
But there is nothing in the API that prevents it.
We'll probably at least move
Variable
toRxCocoa
in the next major version as a good first step.DevAndArtist commentedon Dec 5, 2017
@kzaher even if it's a dangerous pattern in a multi-thread context, how would I append a new item to the value if for instance it was
BehaviorRelay<[Int]>
? What would be correct way to do so? I mean isn't that why we need a thread safevalue { get set }
for in-place mutation?sandeeplearner commentedon Dec 6, 2017
@kzaher and @DevAndArtist : I had exact same issue which I had posted in SO. https://stackoverflow.com/questions/47452582/how-to-use-behaviorrelay-as-an-alternate-to-variable-in-rxswift
One solution proposed by community was to use
BehaviorSubject
but that defeats the purpose.BehaviorRelay
is an alternate toVariable
andVariable's
value property wasmutable
now thatBehaviorRelay
has read only value. I ended up usingaccept
as belowThough it works, I dont think this is the best way to use it. I am sure many are facing this confusion. Please add a sample usage of
BehaviorRelay
to clarify all our doubts.Herbal7ea commentedon Dec 12, 2017
With this change, does BehaviorRelay have to be in RxCocoa? When using this in layers beyond the view (e.g., Presenter/MVVM/Model layers), the best practice is to not include UI imports, but RxCocoa has a lot of UI aspects. Variable was a simple to use piece, that only required RxSwift, but BehaviorRelay breaks that paradigm in it's current library.
freak4pc commentedon Jan 9, 2018
@Herbal7ea The problem is Variable in itself isn't part of RxSwift or ReactiveX. It is a Cocoa-specific implementation that mainly works as an Imperative Bridge to people that find it hard to go "All declarative / all observable" immediately. It is not really a pure component of RxSwift to begin with 🤔
kzaher commentedon Jan 13, 2018
@Herbal7ea
If we extract
*Relay
andSharedSequence
into their own frameworks in the next major version would that solve your concern?The earliest time we can do this is the next major release because of backwards compatibility.
Herbal7ea commentedon Jan 17, 2018
@freak4pc That makes sense. I am mostly concerned about where things are located (RxCocoa), and not the why. @kzaher yeah, that would work. My hopes are mostly to separate the Variable alternative (BehaviorRelay) from any UI grouping. RxCocoa seems to be mostly geared toward working with UI pieces. Thank you both @freak4pc & @kzaher.
nhatlee commentedon Mar 2, 2018
Hi all, please help me the right way to append
value
forBehaviorRelay
. Is the solution of @sandeeplearner correct? I expected another way, because it seem verbose.12 remaining items