Issue
I am trying to get the hang of Room using java in an android app. I've run into an issue, though, where queries are only taking effect after the database has been queried a second time.
For Example: The application has two buttons "Return DB" and "Delete DB." The "Delete DB" button, when clicked, uses a method in the main activity. The method uses an AsyncTask to call the asociated DAO. So say the entity's table currently has 3 records {A, B, C}. When I press "Delete DB" and then press "Return DB," I would expect it to return { }, but in this case, it still returns {A, B, C}. After this though, when I press "Return DB" a second time, it does return { }.
Being new to this, I was just wondering if anyone has ever encountered a similar issue. Thanks!
MainActivity.java
.
.
.
public void deleteDB(View view) {
new DeleteDatabase().execute();
}
.
.
.
private class DeleteDatabase extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
database.myEntityDao().nukeTable();
return null;
}
}
.
.
.
myEntityDao.java
.
.
.
@Query("DELETE FROM MyEntity")
void nukeTable();
.
.
.
Solution
Whoops, sorry, I did a dumb. I was executing the AsyncTask and immediately logging the result, but because the task is done in the background thread, it logged the old data rather than waiting for the new data. I just needed to put the Log statements in the AsyncTask itself.
Answered By - Caleb Whittington
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.