I am writing a android client which pushes data every 20-30 seconds & also receives updates from server asynchronously.
Since i have other UI components as well, I can’t block the main thread.
What are the pros and cons(performance, complexity, maintainability, easy to use API etc..) of using:
- sync i/o with async task / threads for pushing data to & receiving events from server
- async nio lib based i/o
Also suggest if there is a better approach.
With Android and UI apps, you are not only not supposed to block the UI thread, but with newer versions of Android you are simply not allowed to do any networking on the UI thread at all.
Thus, even NIO-based networking on UI thread is usually not a working approach.
For WebSocket, you might want to look at: https://github.com/tavendo/AutobahnAndroid
This does NIO, but on 2 background threads for reader/writer. Notably, it does not do any networking on UI thread .. including connection establishment.
We might rewrite that in the near future to do old-school IO on the background threads since:
a) NIO doesn’t buy a lot in this situation
b) implementing TLS over NIO is a PITA
Disclaimer: I am author of Autobahn and work for Tavendo.