jeudi 14 janvier 2016

Android SQLite query causing many skipped frames

I'm making a few retrofit calls, 4 to be precise, and inside each of these calls I'm storing it's results into a SQLite database, my problem is, these storage queries have usually around 800 entries to save into the database, so it takes a few seconds and it is making the app skip about 300 frames.

So I've searched and found out that the retrofit calls are made in another thread, but it's onResponse, and onFailure are handled by the main thread.

I'm afraid that if I put 4 asyncTasks running on the same time, it wold make the app slow in the same way, am I right? Also because on the end of the Sqlite query I perform a test to determine if that was the last query and show a success message. Any suggestions?

Here's some code:

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 {

                RepositorioCadastro repositorioCadastro = new RepositorioCadastro(conn);

                    // Inserts each item of the list in the sqlite database
                    for (Cadastro c : cadastroList) {

                        repositorioCadastro.inserirCadastro(c);

                    }

                } catch (SQLiteException e) {

                     // On Sqlite Failure
                     Log.i("ACTTOKEN", "Erro ao inserir Cadastro(s) no banco de dados local: " + e.getMessage());

                } finally {

                    // Increments the counter to test if it is the last query to finish
                    contador++;

                    if (contador == 4) {

                        // If is the last query to finish, opens the message box

                        //Increases the progress in a MaterialDialog progressDialog
                        progressDialog.incrementProgress(28);
                        Log.i("CONTADOR", "Contador Cadastros: " + contador);
                        progressDialog.dismiss();// Dispensa a barra de progresso
                        // When click OK in the message box, start a new activity
                        MessageBox.showAlertToken(TokenActivity.this, "Sincronizado com sucesso", "Pressione OK para continuar");

                    } else {
                        //Increases the progress in a MaterialDialog progressDialog
                        progressDialog.incrementProgress(28);
                        contador++;
                        Log.i("CONTADOR", "Contador Cadastros else: " + contador);

                    }
                }

        }

}

@Override
// On Retrofit Failure
public void onFailure(Throwable t) {

    Log.i("ACTTOKEN", "Erro retrofit ao baixar Cadastros: " + t.getMessage());

}
});

Aucun commentaire:

Enregistrer un commentaire