How to know when the Sqlite query ended?

Asked

Viewed 41 times

0

In my app I have a synchronization method, this method downloads several entries from my webservice and inserts them into the local Sqlite database through a for loop. I need to know when the Sqlite insertion is over so I can display a window stating that the operation has been performed successfully.

However, there are many registrations, more than 500, and Sqlite ends up taking a few seconds to enter the database, and with those seconds that take, my code already performs the call of Alerdialog informing the result of the operation.

How can I make Alertdialog only be called when Sqlite insertion is fully completed?

Call the retrofit to download the entries

final CadastroApi cadastroApi = retrofit.create(CadastroApi.class);

Call<List<Cadastro>> callCadastro = cadastroApi.getCadastro(token);
    callCadastro.enqueue(new Callback<List<Cadastro>>() {
        @Override
         public void onResponse(Response<List<Cadastro>> response, Retrofit retrofit) {

            List<Cadastro> cadastroList = response.body();

            if (cadastroList != null) {

                try {

                    DBHelper dbHelper = new DBHelper(TokenActivity.this, token);
                    SQLiteDatabase conn = dbHelper.getWritableDatabase();
                    RepositorioCadastro repositorioCadastro = new RepositorioCadastro(conn);

                    // Insere cada item da lista no Banco de dados
                        for (Cadastro c : cadastroList) {

                            repositorioCadastro.inserirCadastro(c);

                            Log.i("ACTTOKEN", "Cadastro inserido ID: " + c.getId());

                        }

                } catch (SQLiteException e) {
                 Log.i("ACTTOKEN", "Erro ao inserir Cadastro(s) no banco de dados local: " + e.getMessage());
                }

}
  • Beauty Rafael? you could use an Asynctask and just shoot this guy when the query is complete, not to give the feeling that the app crashed you could have a Loader until you finish the operation.

  • Well, I already have a Clicker, it appears when the user clicks on the synchronize button. It turns out that I have 3 more tables in the database that are updated at the same time, each in a different call from the retrofit, if I put an asyncTask for each of the operations, the app drops many frames, around 500...

  • This update together is really necessary?

  • What do you mean? Perform all operations at the same time?

  • this, these data need to be in different tables?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.