-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
BehaviorSubject vs Variable vs other subjects. Public and private read-write/read-only properties. #487
Comments
The reason why variable needs to be transformed using
This is a good and a bad thing.
So in short:
The reason why we have created I would also suggest people to create their own abstractions that express properties they require and wrap observables. We've been recently asked will there be units like My advice is to use the compiler as much as you can, but how individuals use it, it's up to them. I wouldn't want to comment on approaches that external libraries (like Bond) or example apps (ReactiveWeatherExample) take because we haven't been related in any way with them. We've tried to add a lot of example usages to RxExample app, so if there is some question about some of examples there, I would be happy to answer them. |
OK, I think I am a little bit closer to understanding the whole concept. Another question popped in my head while reading your answer. What if I would like to implement something like Promise pattern? Meaning I have some async tasks that I need to have executed serially dependent on each other, obviously I can achieve this by I see, What is best practice in FRP with RXSwift then? Having all properties in classes, controllers, view models as |
The answer to first question is actually All of these things you have mentioned are equivalent to observable sequences, and thus composable. I would probably prefer We've tried to explain and provide examples how one could best use other concepts in RxExample app. I'm not sure there is a better way how I can explain where to use which concept other then those examples and all documentation we have. The general rules of thumb are:
|
Some exposed properties are supposed to have read-write access, Other read-only and the rest of them are private. So possibly for public read-only case I could do something like:
Would that be correct approach? |
Hi @zdenektopic , your suggestion will probably work, but this is not idiomatic for sure and not an approach I would suggest people to take. This is one of the examples in the example app: It also has an explanation
There is also the same example using vanilla observable sequences: These are kind of ideal examples IMHO. Important things:
If you are more into Redux kind of architectures, take a look at calculator example: https://github.com/ReactiveX/RxSwift/tree/master/RxExample/RxExample/Examples/Calculator |
Thanks, helped me a lot to understand how ViewModels should be built using RxSwift! :) If you have some commands/actions in view model that come from view controller, you have to have |
Hm, why do you think you need to Word |
This article uses behavior subjects extensively. Would you say that is not the ideal implementation? https://medium.com/cobe-mobile/implementing-mvvm-in-ios-with-rxswift-458a2d47c33d#.oucl7alcp |
@mingyeow I think that code could be improved, so yes, it's probably not the most ideal implementation IMHO. |
We can probably close this one and reopen if needed. |
Hi @kzaher! |
I will second to @serejahh, in real world application you most likely want to create ViewModel outside of the ViewController due to dependencies coming from somewhere else that needs to be DI'ed to the ViewModel itself before the viewDidLoad is called For very simple applications the approach works fine when viewModel itself can be constructed without any external dependencies but thats not the case with most of the real world applications I think for that either Subject or Variable seems the only way to model this use-case, although I would only expose them as observables and keep the rest internal to viewModel |
@serejahh @Vasant-Patel A good way to get around the need for a loaded view before initing your view model is described in this blog post - see the It allows the ViewModel to be created in a closure passed to the init of the ViewController like so: let viewController = AuthenticationLandingViewController.create { (viewController) -> AuthenticationLandingViewModelType in
let viewModel = AuthenticationLandingViewModel(viewActions: viewController.actions)
// any extra config of the VM here
return viewModel
} |
@sergdort this is great. |
I'm a bit late to the conversation, and I haven't read the blog post from @iandundas or the gist from @sergdort , but I'd like to share my current approach: I'm using Swinject and passing down a |
Hello.
I will start with what I know.
If I understand it correctly,
Variable
is type that allows storing value (read-write access) and wrapsBehaviorSubject
which I have no idea what is for. AndObservable
is type that allows read-only access.My issue is regarding combineLatest operator which in
Rx.playground
allows to combine multipleBehaviorSubject
s, but notVariable
s.Variable
s can be alsoObservable
s visasObservable()
method. Is there any difference betweenBehaviorSubject
andVariable
? Which should I use for common storing of values and objects in myUIViewController
s? Or should I use otherSubject
?What if I want to have publicly read-only
Variable
/other subject/Observable
and other read-write also public?Another thing is
Driver
or other Units. Should I Always useDriver
when working with UI elements? How? Should I haveVariable
or otherSubject
and useasDriver()
every time I need all shit going on UI/main thread? In ReactiveWeatherExample there is notDriver
used inUIViewController
and it is instead manually ensured, the code executes on UI thread.For example in Bond you use Observable type to for all - storing values and subscribing for events/new values.
My current code with combineLatest:
The text was updated successfully, but these errors were encountered: