-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Add ViewActions utilities for Android #1584
Conversation
RxJava-pull-requests #1495 SUCCESS |
Does this replace or add to #1545 ? Can you provide comments about this pull request such as what it's doing, why it's being proposed, if it affects any existing code, etc? |
It's completely separate from that request. These are just common Actions provided as utility for convenience. Shouldn't affect any code - it's all brand new. One common use case: Observable<Boolean> isField1Valid = // ...
Observable<Boolean> isField2Valid = // ...
Observable.combineLatest(isField1Valid, isField2Valid)
.subscribe(setClickable(submitButton)); |
|
||
public class ViewActionSetActivated implements Action1<Boolean> { | ||
|
||
private View view; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can make these final everywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch - fixed here: https://github.com/ronshapiro/RxJava/commit/5bc9ac13affae0ea919742b41c20903866221b42
So in general, this is really useful to have and I had started doing something similar in a side project, where I used Rx more in the UI domain. One general thing I don't like about the implementation is that it's based on This means that whenever your upstream observable fails, that error will get swallowed. I guess alternatively one could do this:
For some reason I think that a solution using Maybe I'm looking at it wrong, just something that occurred to me. |
PS: I know that side effects should usually occur in |
LGTM. I also had some part of 'ViewObservers' written just for particular project, so it will be good to have provded ones. @mttkay I think than using Action1 is Ok, in your provided example
you can use subscribe method with 2 separated actions for onNext and onError
|
LGTM @ronshapiro good job! :) |
@mttkay I'm not sure how you would plan on using Observable.from(true, false)
.map(setVisibility(myView); // this never gets triggered because there's nothing subscribing. That being said, these are simple classes, and they could easily also implement As for error propagation, you can always add an |
Just a thought... Given that the Action references a View at the end of the subscription chain for example Observable.from(...).subscribe(setClickable(view); I wonder if these actions shouldn't reference the respective views weakly given the likelihood of upstream async operations? Unsubscribe should take care of it but given that the process of unsubscribing is manual I wonder if giving this extra "help" to avoid context leaks would be interesting? Thoughts? |
RxJava-pull-requests #1500 SUCCESS |
@dpsm I'd hope that these |
@ronshapiro All Views leak the parent Context one way or another :( |
@ronshapiro fair point, let's use Action1 then @dpsm has a good point there; all views strongly reference the Activity they're created in (we already had a lot of fun with this when working on I can see this becoming necessary more and more going forward; maybe it would even make sense to provide a wrapper around this, e.g. a |
@mttkay can you elaborate on "bindFragment and its various failed attempts at dealing with this". I'd love to help fixing that. In general I like the idea of ViewObservables.weakView(..) or .bindView(..) that trigger a child observable and then holds a weak reference to the View. Did I get your idea right? |
@dpsm The discussion is spread across numerous issue reports and pull requests, but these searches should be a decent starting point: https://github.com/Netflix/RxJava/search?q=fromFragment&type=Issues (bindFragment used to be called fromFragment and work entirely different) It's hard or impossible to pro-actively prevent context leaks in subscribers, since Android does not give you enough or even consistent information (hooks) to get notified about context life-cycle events such as activity destruction. WeakReferences usually don't work, since Rx is heavily based on lambdas and short lived objects, so a subscriber that's passed as a closure would get garbage collected immediately. TLDR is: We must keep strong references to subscribers, but we must not keep strong references to subscribers. :-) Any help welcome in fixing this! |
No description provided.