wasync icon indicating copy to clipboard operation
wasync copied to clipboard

Network activity on UI thread on Android

Open dereulenspiegel opened this issue 11 years ago • 3 comments

When creating a connection in Android activities in onCreate (like shown here http://jfarcand.wordpress.com/2013/04/04/wasync-websockets-with-fallbacks-transports-for-android-node-js-and-atmosphere/) the exception android.os.NetworkOnMainThreadException is thrown. wasync seems to invoke networking methods not on in a background thread. While it is possible and trivial to do the connection set up in the background the expectation is that an synchronous librarylike wasync does all networking in a background thread. This would simplify the usage of this library.

dereulenspiegel avatar Apr 19 '13 15:04 dereulenspiegel

Hi, can you elaborate? wAsync is fully asynchronous, hence I needed to add those android property. How can it be avoided?

jfarcand avatar Apr 19 '13 15:04 jfarcand

On newer versions of Android there is a policy which prohibits all network activity on the UI thread. In case you call any method related to networking like opening sockets or using URLConnection.open an exception is thrown. It seems that wAsync does some networking on Socket.open before everything else is handled in the background. On other platforms this might not be a problem, but Android is very strict with these things. Threading in general is a bit tricky, since the results must be delivered back to the UI on the UI thread. In the long run it could be nice for developers to have some kind of "wrapper" for wAsync on Android which handles threading and delivery of results in the correct threads.

dereulenspiegel avatar Apr 19 '13 15:04 dereulenspiegel

I see. If you have the cycle, do a pull request with your idea and I will start from there.

jfarcand avatar Apr 19 '13 16:04 jfarcand