OneSignal-Flutter-SDK icon indicating copy to clipboard operation
OneSignal-Flutter-SDK copied to clipboard

Fatal Exception Methods marked with @UiThread must be executed on the main thread

Open uj opened this issue 7 years ago • 28 comments

Description:

No changes to any code, other that updating Flutter to the lastest dev and master channels.

Using the dev channel of flutter, using OneSignal causes this error:

E/AndroidRuntime( 7963): FATAL EXCEPTION: OSH_NetworkHandlerThread E/AndroidRuntime( 7963): Process: com.myapp.myapp, PID: 7963 E/AndroidRuntime( 7963): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: OSH_NetworkHandlerThread E/AndroidRuntime( 7963): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794) E/AndroidRuntime( 7963): at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:727) E/AndroidRuntime( 7963): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:140) E/AndroidRuntime( 7963): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:225) E/AndroidRuntime( 7963): at com.onesignal.flutter.OSFlutterChangeTagsHandler.onSuccess(OneSignalTagsController.java:43) E/AndroidRuntime( 7963): at com.onesignal.UserStateSynchronizer.internalSyncUserState(UserStateSynchronizer.java:196) E/AndroidRuntime( 7963): at com.onesignal.UserStateSynchronizer.syncUserState(UserStateSynchronizer.java:170) E/AndroidRuntime( 7963): at com.onesignal.UserStateSynchronizer$NetworkHandlerThread$1.run(UserStateSynchronizer.java:77) E/AndroidRuntime( 7963): at android.os.Handler.handleCallback(Handler.java:790) E/AndroidRuntime( 7963): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 7963): at android.os.Looper.loop(Looper.java:164) E/AndroidRuntime( 7963): at android.os.HandlerThread.run(HandlerThread.java:65)

I believe that this is related to the following new change in flutter, and it has affected other plugins as well:

https://github.com/flutter/engine/commit/2c9e37c34e79475bbde7c8163eb5e56cdb9662a1

This appears to be the same issue as https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/90 and https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/95

Both of those issues (not mine) were closed not being able to reproduce, but I think you have to be on Flutter dev or master to reproduce it.

uj avatar Jun 22 '19 19:06 uj

Hello, have you attempted changing the channel to 'stable' as mentioned in #90 ?

rgomezp avatar Jun 25 '19 20:06 rgomezp

Yes, stable works fine, but there are 2 issues with that:

1 The change made in the beta/dev/master that is causing this problem is an intentional change, not a bug, and it will soon be pushed to the stable branch (I think they said it would be in July). So, using Stable is just pushing this issue down the road a few weeks instead of fixing it.

2 Also, for Android, Flutter is recommending using the dev/master branch to create builds because the stable branch is not creating the 64-bit versions properly to be accepted by the PlayStore.

uj avatar Jun 25 '19 21:06 uj

I'm not sure exactly what the "Help Wanted" label is referring to, but there have been many plugins affected by this lately, and most of them have already been fixed now. There are several conversations in the Flutter github with the flutter people helping the plugin makers to fix their plugins regarding this error message.

uj avatar Jun 28 '19 18:06 uj

I am not a Java developer but following other plugins, wrapping the native call in a Looper Handler works for me on flutter beta(1.6.3) https://github.com/kaashyapan/OneSignal-Flutter-SDK

I also upgraded Gradle to 3.3.2. Happy to submit a PR if this can help.

kaashyapan avatar Jun 30 '19 11:06 kaashyapan

I've tried changing the SDK version to 1.6.3. This works fine on my device (Redmi 4x) but is still Force Close on other devices. Is there any help? Because I have to use the dev channel for some reason

Thanks

rikiyanuar avatar Jul 06 '19 07:07 rikiyanuar

@rikiyanuar Look at @kaashyapan fork, that one has the fix in it. Hopefully this will be implemented into the OneSignal official build, but there has been no official statement yet from them.

uj avatar Jul 08 '19 23:07 uj

i had the same problem after upgrading the flutter to v.1.7.8+hotfix.2

rymesaint avatar Jul 09 '19 04:07 rymesaint

I also have the same situation with the last update v.1.7.8+hotfix.2

marrullas avatar Jul 09 '19 18:07 marrullas

I wish that OneSignal would give an official answer on this, other than just saying that the stable version works so it isn't a problem ... knowing that it was only a few days before the stable would be bumped up to the beta, which had the problem. And... here we are, the stable has been bumped up, so of course it doesn't work now.

Anyway, try using the @kaashyapan fork for now, it should be fixed there.

uj avatar Jul 09 '19 18:07 uj

@uj thanks for advice. But I've tried the @kaashyapan fork but it still Force Close on my phone, which SDK version do you use for @kaashyapan fork?

rikiyanuar avatar Jul 10 '19 02:07 rikiyanuar

this is working for me https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/90#issuecomment-494901091

xXUlquiScXx avatar Jul 10 '19 03:07 xXUlquiScXx

@uj thanks for advice. But I've tried the @kaashyapan fork but it still Force Close on my phone, which SDK version do you use for @kaashyapan fork?

flutter beta(1.6.3).

Post your exception messages here if it is still a problem.

kaashyapan avatar Jul 10 '19 03:07 kaashyapan

@rikiyanuar Are you getting the "@UiThread must be executed on the main thread" error message? It doesn't sound like you are, because that will stop it from building. You can't have a force close if you can't even build to begin with. If you are not getting that error message, then you should start another thread.

https://ucarecdn.com/51902472-9a6b-4f28-86bf-16ba2a886514/photo6138462419194849880.jpg

I really don't understand how it works, but the picture above is a screenshot of my phone. If my problem is not relevant for this thread, I'm sorry. I'll try to find my own ways.

Thanks for your advice Thanks @kaashyapan for the fork

rikiyanuar avatar Jul 10 '19 05:07 rikiyanuar

@rikiyanuar Sorry, I mis-wrote earlier. Yes, you have the problem described. If that fork doesn't work, then you have these 3 options: Try this other workaround https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/90 or wait for OneSignal to update their sdk, or go back to the previous 1.5.x stable release of Flutter.

uj avatar Jul 10 '19 05:07 uj

Confirmed not working with flutter version v1.7.8-hotfix.3 on stable channel. Going back to 1.5.x solves the issue. The fork from @kaashyapan doesn't work for me.

MarcoAltranDC avatar Jul 10 '19 17:07 MarcoAltranDC

After updating to 1.7.8-hotfix.3 on stable I am also getting the same error. I am assuming it affects everyone who is using onesignal now. Please fix this on priority.

getmmg avatar Jul 11 '19 10:07 getmmg

Does anyone have a solution for this? Any workaroud?

@Nightsd01 please help!!

thiagotalma avatar Jul 11 '19 13:07 thiagotalma

E/AndroidRuntime(30306): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: OS_GETTAGS_CALLBACK E/AndroidRuntime(30306): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794) E/AndroidRuntime(30306): at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:727) E/AndroidRuntime(30306): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:140) E/AndroidRuntime(30306): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:225) E/AndroidRuntime(30306): at com.onesignal.flutter.OSFlutterChangeTagsHandler.tagsAvailable(OneSignalTagsController.java:63) E/AndroidRuntime(30306): at com.onesignal.OneSignal$15.run(OneSignal.java:1697) E/AndroidRuntime(30306): at java.lang.Thread.run(Thread.java:764)

Init methods and subscribe methods doesn't seem to produce the crash. Anything related to tags (read/write) produce the ui thread error.

getmmg avatar Jul 11 '19 14:07 getmmg

Does anyone have a solution for this? Any workaroud?

@Nightsd01 please help!!

For now, work with a previous version command: flutter version v1.5.4-hotfix.2 <--- working

marrullas avatar Jul 11 '19 15:07 marrullas

OneSignal please fix that, thanks. [Edit: from what I gather the developer in charge of this plugin left OneSignal. :-/]

laurent-humbert avatar Jul 15 '19 13:07 laurent-humbert