I have a database, ROOM
, which makes a select shortly after returning a asyncTask
. I get the return and I make a select
, waiting for the return of this select
.
It works normally, at the first iteration of for
, but from the second onwards, the return is always null. The data is in the database and should rather return me.
If I do this by ALT + F8 , the data returns me in the query. But by for
, it is null.
Why does this happen?
AppDataBase:
private AppDataBase appDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atendimento_tab);
appDataBase = AppDataBase.getInstance(this);
}
@Database(entities = {CidVo.class}, version = 8, exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {
public abstract CidDao getCidDao();
private static AppDataBase appDataBase;
public static AppDataBase getInstance(Context context) {
if (null == appDataBase) {
appDataBase = buildDataBaseInstance(context);
}
return appDataBase;
}
private static AppDataBase buildDataBaseInstance(Context context) {
return Room.databaseBuilder(context,
AppDataBase.class,
"AutoCompleteVo")
.fallbackToDestructiveMigration()
.allowMainThreadQueries().build();
}
//CID predicted
public CidVo getCid(Context context, String idCidVo) {
if (appDataBase == null) {
appDataBase = AppDataBase.getInstance(context);
}
return appDataBase.getCidDao().getCidVo("%" + idCidVo + "%");
}
}
DAO:
@Dao
public interface CidDao {
//CID
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllCID(List<CidVo> cidVos);
@Query("Select * from CidVo WHERE idCid = :idCid")
CidVo getCidVo(String idCid);
}
FOR:
@Override
public void retornoAsyncTaskResultPredict(AsyncTaskResult<Retorno> asyncTaskResult) {
if (asyncTaskResult.getExceptionResult() == null) {
RetornoPredicaoCid predicaoCidVo = (RetornoPredicaoCid) asyncTaskResult.getResult();
if (predicaoCidVo.getRetorno() != null) {
List<PredicaoCidVo> predCid = predicaoCidVo.getRetorno();
List<CidVo> predictedText = new ArrayList<>();
List<CidVo> predictedText1 = new ArrayList<>();
for (int i = 0;i<predCid.size();i++) {
//appDataBase = AppDataBase.getInstance(this);
CidVo cidVo = appDataBase.getCidDao().getCidVo(predCid.get(i).getTextPredicted());
CidVo cidVo1 = appDataBase.getCid(this, predCid.get(i).getTextPredicted());
predictedText1.add(cidVo1);
predictedText.add(cidVo);
}
}
}