Comments (4)
Why not just:
mailLoginButtonTaps.asObservable()
.flatMap {
authenticationService.login(withEmail: email.value, password: password.value)
}
.asDriver(onErrorJustReturn: false)
Also, this is not just for the readability. It's more about responsibility. If using second code, View calls emailLogin()
, then the View will have a responsibility to handle the LoginResult
data to map to UI components. This means, View has control flow. This is against the first rule: View doesn't have control flow. View cannot modify the data. View only knows how to map the data.
With the first code, the whole process of login and the result are handled by ViewModel. Then you can expose the specific data what view need.
ViewModel
let loggingIn = mailLoginButtonTaps.asObservable()
.flatMap { in
authenticationService.login(withEmail: email.value, password: password.value)
}
self.errorMessageLabelText = loggingIn
.catchError { error -> Driver<String> in
Driver.just("\(error)")
}
self.presentNextViewController = loggingIn
.flatMap { result in
return // ...
}
from rxtodo.
I appreciate your input. I am learning the optimal way to implement MVVM + RxSwift architecture and your sample project has certainly helped.
Firstly,
The reason why I did this
.withLatestFrom(Observable.combineLatest(email.asObservable(), password.asObservable()) { ($0, $1) })
is that email and password is declared as member variables,
hence if I were to copy+paste your codes, adding the missing required self.:
authenticationService.login(withEmail:*self*.email.value, password: *self*.password.value)
this will in turn produces a compilation error: self captured by a closure before all members were initialized.
If I were to move this binding out of the init()
This won't work anymore,
self.errorMessageLabelText = loggingIn
since errorMessageLabelText has to be initialized in init(), and it requires loggingIn
I'm trying to following your advice for separating responsibilities, however, with all these code jugglings, I ended up with twice the amount of codes and am worried about the long-term readability and maintananibility of the codes.
from rxtodo.
I'm looking at 2 other MVVM+RxSwift projects which does not seem to enforce this rule strictly.
https://github.com/artsy/eidolon
https://github.com/ivanbruel/SwipeIt
Your thoughts? Or do you know of other open-source projects that managed to implement your coding guidelines successfully?
from rxtodo.
-
Variable
s areclass
. You can declare those first than assign to properties:ViewModel
init() { // declare local variables let email: Variable<String> = ... let password: Variable<String> = ... // do with local variables authenticationService.login(withEmail: email.value, password: password.value) // assign to properties self.email = email self.password = password }
-
I understand what you're worrying about. I have same worries too. I'm always thinking and thinking to write a good code. What I think the good code should be is: responsibility-separated, high-readable, long-term-maintainable and easy-testable.
What I wrote in Philosophy section is just a result of my philosophical speculation, and those are closer to 'Guidelines' than the 'Rules'. So please don't be restricted too much in guideline because there are always exceptions in the real world. (The only way to not make exceptions is not to write a code).
PS: I think there're no open sourced apps using my guideline, but I'm developing social media app with these guidelines.
from rxtodo.
Related Issues (13)
- empty text field HOT 1
- Every task as a section 🤔 HOT 4
- Why TaskListViewModel is dealloc? HOT 3
- Memory Leak? HOT 4
- Mutate Action to Observable<Mutation> HOT 2
- edit mode : state initialisation not sync with input text HOT 2
- Cell State bind and distinctUntilChanged problem HOT 2
- Distinct until changed HOT 3
- Multiple Sections
- Type 'RxTableViewSectionedREloadDataSource' has no member 'Section'
- Can't run pod install HOT 1
- Not work HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxtodo.