Good day to all! I had such a question, the answer to which, unfortunately, I did not find ... All Android developers strongly recommend to perform absolutely ALL database operations in a separate thread (use AsyncTask or AsyncTaskLoader ). With the operation of obtaining data, everything is clear here, it is really possible to load the system with a huge number of lines, so here the use of loaders is quite reasonable. But I am writing a simple application (ordinary checklist), in which there are two simple tasks:

  • When installing the checkbox of the corresponding task, set the IsDone = true | false value for the string to the database (that is, a simple query like UPDATE Tasks SET IsDone = true WHERE TaskId = 10 )

  • Add a task to the database from a separate activation and close it. (Naturally, user interaction with the activation is not needed here, as long as the line "is added").

So, the question is as follows. Is it necessary for each such simple database queries to sculpt their own AsyncsTasks separately, suffer from the synchronization of streams, and thereby turn their code into "vermicelli" from AsyncTasks ? I would like to know the opinion of experienced developers. Do you always put queries to the database in separate threads, or do you leave such simple queries (which I cited as an example) in a UI thread?

  • if the task: to change only true | false, then you can use SharedPreferenses without asyntasks and runOnUIThread - Kirill Stoianov
  • one
    Yes, all IO operations need to be done in separate threads. In the company where I work the last year we do it with the help of RxJava , a very cool thing, though the subtleties and pitfalls in it are not few. But if you look at it, you can do amazing things with very compact code, especially in conjunction with Retrolambda . And before we got hooked on RxJava, we used Robospice . That's the way a small reviewer on the topic. - xkor

0