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 ?
Asked
Active
Viewed 966 times
1

Okan
- 1,379
- 3
- 25
- 38
1 Answers
1
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.

koush
- 2,972
- 28
- 31
-
No, you want the callback to contain the asynctask. doInBackground will immediately return since ion is async. – koush Jan 08 '15 at 22:14
-
What do you suggest ? – Okan Jan 08 '15 at 22:15
-
Can you give a little example for handler ? I know the handlers but I didn't understand how to use in this case. – Okan Jan 09 '15 at 15:58
-
1Thx @koush, with just 1 comment, that method is '.setHandler(null)' and we can make this just after '.load(...)' – Mateusz Pryczkowski Sep 09 '16 at 09:59