Atmosphere / wasync

WebSockets with fallback transports client library for Node.js, Android and Java
http://async-io.org
161 stars 47 forks source link

Network activity on UI thread on Android #30

Open dereulenspiegel opened 11 years ago

dereulenspiegel commented 11 years ago

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.

jfarcand commented 11 years ago

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

dereulenspiegel commented 11 years ago

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.

jfarcand commented 11 years ago

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