As the title says, android needs queries out of main thread since it will trhow java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time otherwise. So I managed to make async queries as many tutorials explain, but it doesn't make so much sense (so far) as I could achieve.
public class NewDetalleDiarioActivity extends AppCompatActivity {
    @Override
    protected void onStart() {
        super.onStart();
        db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database").build();
        findPeriodo();
        findDiario();
    }
    private void findPeriodo() {
        periodo = Diarios.getPeriodo(db);
        if (periodo == null) {
            Intent intent = new Intent(NewDetalleDiarioActivity.this, NewPeriodoActivity.class);
            startActivity(intent);
        }
    }
PROBLEM/ERROR:
If periodo is null, another activity is started, otherwise this one continues its thread.
The problem is that, when I debug it (which slows proceses, of course) periodo returns an instance from the database, but when I run the code without debugging, periodo is null.
public class Diarios {
    public static Periodo getPeriodo(AppDatabase db) {
        return Factory.getIntPeriodo().getPeriodo(db);
    }
}
.
public class Factory {
    private static IntPeriodo intPeriodo;
    public static IntPeriodo getIntPeriodo() {
        return (intPeriodo == null) ? intPeriodo = new BusPeriodo() : intPeriodo;
    }
}
.
public class BusPeriodo implements IntPeriodo {
    // I don't think it's necessary to post the interface...
    @Override
    public Periodo getPeriodo(final AppDatabase db) {
        final Periodo[] periodo = new Periodo[1];
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() { //the async query that is driving me mad.
                periodo[0] = db.periodoDao().getPeriodo(new Date());
            }
        });
        return periodo[0];
    }
}
What's the proper way to make select queries without getting them delayed?
The select query is indeed working, I don't think is necessary to post it (because it is returning an unique result when I debug), but it returns null when I run the code without debugging!! Please help.
