Skip to content
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

Force ViewObservable be subscribed and unsubscribed in the UI thread #880

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package rx.android.observables;

import android.os.Looper;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
Expand All @@ -37,5 +38,10 @@ public static Observable<Boolean> input(final CompoundButton button, final boole
return Observable.create(new OperatorCompoundButtonInput(button, emitInitialValue));
}

public static void assertUiThread() {
if (Looper.getMainLooper() != Looper.myLooper()) {
throw new IllegalStateException("Observers must subscribe from the main UI thread, but was " + Thread.currentThread());
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.Scheduler.Inner;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import android.app.Activity;
import android.os.Looper;
import android.util.Log;
Expand Down Expand Up @@ -100,8 +102,15 @@ public void onNext(T args) {
@Override
public void call() {
log("unsubscribing from source sequence");
releaseReferences();
sourceSub.unsubscribe();
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {

@Override
public void call(Inner t1) {
releaseReferences();
sourceSub.unsubscribe();
}

});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.Scheduler.Inner;
import rx.android.observables.ViewObservable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import android.view.View;
import android.widget.CompoundButton;

Expand All @@ -39,6 +43,7 @@ public OperatorCompoundButtonInput(final CompoundButton button, final boolean em

@Override
public void call(final Subscriber<? super Boolean> observer) {
ViewObservable.assertUiThread();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is ViewObservable? Did that land in another PR?

I would prefer to not have this call here. What we want to do instead is, if at all, check this in the fromFragment / fromActivity helpers (see discussion in #754 which asks to remove this assertion entirely)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to make sure that subscribe happens in the UI thread or there would be some concurrent issues. So I add assertUiThread here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just add it in the helpers, users still can subscribe in other threads after they get the Observable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ViewObservable is from #783

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "some concurrent issues"? We've had no issues so far

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that create an Observable in the UI thread and subscribe it on other thread. For example, Schedulers.newThread()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g., ViewObservable.clicks(..).subscribeOn(Schedulers.newThread()).subscribe(...)

final CompositeOnCheckedChangeListener composite = CachedListeners.getFromViewOrCreate(button);

final CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
Expand All @@ -51,7 +56,14 @@ public void onCheckedChanged(final CompoundButton button, final boolean checked)
final Subscription subscription = Subscriptions.create(new Action0() {
@Override
public void call() {
composite.removeOnCheckedChangeListener(listener);
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {

@Override
public void call(Inner t1) {
composite.removeOnCheckedChangeListener(listener);
}

});
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.Scheduler.Inner;
import rx.android.observables.ViewObservable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
Expand All @@ -35,6 +39,7 @@ public OperatorEditTextInput(final EditText input, final boolean emitInitialValu

@Override
public void call(final Subscriber<? super String> observer) {
ViewObservable.assertUiThread();
final TextWatcher watcher = new SimpleTextWatcher() {
@Override
public void afterTextChanged(final Editable editable) {
Expand All @@ -45,7 +50,14 @@ public void afterTextChanged(final Editable editable) {
final Subscription subscription = Subscriptions.create(new Action0() {
@Override
public void call() {
input.removeTextChangedListener(watcher);
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {

@Override
public void call(Inner t1) {
input.removeTextChangedListener(watcher);
}

});
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import java.util.WeakHashMap;

import rx.Observable;
import rx.Scheduler.Inner;
import rx.Subscriber;
import rx.Subscription;
import rx.android.observables.ViewObservable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import android.view.View;

public final class OperatorViewClick implements Observable.OnSubscribe<View> {
Expand All @@ -38,6 +42,7 @@ public OperatorViewClick(final View view, final boolean emitInitialValue) {

@Override
public void call(final Subscriber<? super View> observer) {
ViewObservable.assertUiThread();
final CompositeOnClickListener composite = CachedListeners.getFromViewOrCreate(view);

final View.OnClickListener listener = new View.OnClickListener() {
Expand All @@ -50,7 +55,14 @@ public void onClick(final View clicked) {
final Subscription subscription = Subscriptions.create(new Action0() {
@Override
public void call() {
composite.removeOnClickListener(listener);
AndroidSchedulers.mainThread().schedule(new Action1<Inner>() {

@Override
public void call(Inner t1) {
composite.removeOnClickListener(listener);
}

});
}
});

Expand Down