-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Proposal/rehash: Use async Messaging #416
Changes from 9 commits
49987ee
b9bc520
e1e680f
0a89470
2cbf877
5f29581
da14acf
ac213bb
2e64d32
551b7b4
2d46b26
c9e2bbf
69cffc3
3fa20bb
6750639
81ac21a
d9a51a7
6fbdff3
1c1182b
1fe6366
4ef9c53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,20 +13,21 @@ | |
*/ | ||
package io.reactivex.android.schedulers; | ||
|
||
import android.annotation.TargetApi; | ||
import android.os.Build; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
import io.reactivex.Scheduler; | ||
import io.reactivex.android.plugins.RxAndroidPlugins; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.concurrent.Callable; | ||
|
||
/** Android-specific Schedulers. */ | ||
public final class AndroidSchedulers { | ||
|
||
private static final class MainHolder { | ||
|
||
static final Scheduler DEFAULT = new HandlerScheduler(new Handler(Looper.getMainLooper())); | ||
static final Scheduler DEFAULT | ||
= new HandlerScheduler(createAsyncHandler(Looper.getMainLooper())); | ||
} | ||
|
||
private static final Scheduler MAIN_THREAD = RxAndroidPlugins.initMainThreadScheduler( | ||
|
@@ -47,6 +48,28 @@ public static Scheduler from(Looper looper) { | |
return new HandlerScheduler(new Handler(looper)); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.P) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree! But lint fails without it despite the if-check :| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try just using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, yeah using just 28 worked. c9e2bbf RIP dessert club |
||
private static Handler createAsyncHandler(Looper looper) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
return Handler.createAsync(looper); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
try { | ||
//noinspection JavaReflectionMemberAccess | ||
return Handler.class | ||
.getConstructor(Looper.class, Handler.Callback.class, boolean.class) | ||
.newInstance(looper, null, true); | ||
} catch (IllegalAccessException e) { | ||
return new Handler(looper); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just make these all empty and hoist a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch 551b7b4 |
||
} catch (InstantiationException e) { | ||
return new Handler(looper); | ||
} catch (NoSuchMethodException e) { | ||
return new Handler(looper); | ||
} catch (InvocationTargetException e) { | ||
return new Handler(looper); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should recycle the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
private AndroidSchedulers() { | ||
throw new AssertionError("No instances."); | ||
} | ||
|
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.
This seems like it's going to require a major version update. The implications to the entirety of the world which uses RxAndroid are scary and a silent change in a minor release doesn't seem appropriate.
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.
I agree, but a major change would require breaking with keeping the same major version as rxjava :|.
Either that or maybe do an extended preview release and ask for feedback?
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.
I think we overload
from(Looper)
with a version that takes a boolean forasync
just like theHandler
constructor no matter what.As to getting it into
mainThread()
, you can use theRxAndroidPlugins
to install an init callback for its creation which you can then delegate tofrom(Looper.getMainLooper(), true)
.I think that's the minimum-viable API to at least get this into a release now for people to start testing it. And from there we can determine whether or not it's even possible to make it the default behind
mainThread()
in the next major version–or sometime in the future–and whether or not we need to expose avsyncMainThread()
or similar accessor to a singleton instance for code which truly needs the old behavior.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.
That works too! For some reason I had it in my head that you were against having support for both. Can update. Should I add a brief mention/example in the readme with it or save that for changelog?