Search code examples
androidandroid-ion

Ion library and async operations


I am using ion library for network operations in my app. (https://github.com/koush/ion) I have a question about this.I am downloading data from my server with ion,and in onComplete method I am saving this datas to app database.Do I need use a new thread for this db operations ?


Solution

  • ion invokes callbacks onto the UI thread by default. Doing db operations on the ui thread is not recommended.

    If you use .handler(null) during your ion request, it will invoke the callback on the network i/o thread that ion uses, and you can use that thread to do a db operation. If the db operation takes too long, it will block other network operations though. It's fine to use, so long as it isn't exceedingly long, and will avoid UI jank.

    Alternatively, use a background thread, or preferably a Looper specifically for db operations, providing the Handler object to the handler method during the request build.