-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
HeadlessJsTaskService is expected to run on UI thread #15915
Comments
starting a headless task is expected to run on UI thread: react-native/ReactAndroid/src/main/java/com/facebook/react/HeadlessJsTaskService.java Lines 89 to 96 in 1e8f3b1
|
@chirag04 thanks for the note. However, as I understand, one of the common uses of this is performing tasks triggered by a push notification (or a job schedule). These are clearly not required to run in a UI thread as these are background activities. |
@oriharel according to the documentation, HeadlessJS is meant for tasks that are needed to be ran when your app in the background. In which case there are no operations on the main thread.
|
Exactly. This is why I don't understand the "startTask must be triggered from the UI thread" requirement. Unless there is some limitation forcing the React context to be spawned from the UI thread. |
That means you can not trigger a headless task from another headless task (threads of threads). |
In the documentation, it is clearly stated -
Which means, even if a BroadcastReceiver is triggered, say from a push notification, the Headless task should be able to perform. But this is not the case, because there is an underline requirement that the onReactContextInitialized event handler will be called from the main thread (AKA UI thread). A requirement that most of the time not fulfilled (for example, when a user swiped out the app from the recent apps menu). So in fact - I DO consider this a bug and updated the form. |
+1 |
2 similar comments
+1 |
+1 |
I think there's a bit of confusion here. The "UI" thread is simply the "main" thread of an Android application. It's the thread that |
If you're on a background thread it should be fairly easy to post something to the main one and call |
You're right, everything I said about the UI thread actually reffers to the main thread. The Service "onStartCommand" does indeed happen on that thread. However, inside startTask there is an asynchronic call to create the React context. The callback that is triggered asynchronically is no longer on that thread. And that is already a private area so we can't override this in our inheriting class. |
An updated pull request here. |
Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> Headless tasks are required to run in the main thread, however due to the nature of the React context creation flow, the handler may be returned outside of the main thread, causing the HeadlessJsTaskContext to throw an exception. Swipe out the app. send push notification from a server that starts a HeadlessJsTaskService Closes #15940 Differential Revision: D5852448 Pulled By: foghina fbshipit-source-id: 54c58a1eb7434dd5de5c39c28f6e068ed15ead9d
+1 |
Issue is now fixed and merged to React Native. |
I don't think it's released yet |
Check your AppRegistry in js code. You need to register a js file before you call it from Java |
Also check your HeadlessJsTaskService implementation in Java. Make sure you return new HeadlessJsTaskConfig with a name "exactJobKey". |
Thanks. Looks like this issue is fixed in 0.49.5. |
Is this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
react-native -v
: react-native-cli: 2.0.1 react-native: 0.48.1node -v
: v7.5.0npm -v
: 5.3.0Steps to Reproduce
com.facebook.react.bridge.AssertionException: Expected to run on UI thread!
Expected Behavior
I expect no UI thread requirement for a background service (same as Android native SDK)
Actual Behavior
App throws an exception
The text was updated successfully, but these errors were encountered: