As discussed here Android Room - Get the id of new inserted row with auto-generate it is possible to get the ID from the newly inserted object.
But how do I get this ID all the way from the DAO to my Activity class in an MVVM pattern?
Some answers on there suggest using rxjava and something like this:
return Single.fromCallable<Long> { recipesDao.insertManual(cookingRecipes) }
But does it make sense to implement two whole Gradle dependencies just to have one method call?
Isn't there a way to make this work with my current structure?
Activity - ViewModel - Repository ( - AsyncTask) - Dao
The only problem is getting the return value from the insert statement, which is called from an AsyncTask
:
private static class UpdateChallengeInfosAsyncTask extends AsyncTask<ChallengeInfos, Void, Void> {
private ChallengeInfosDao mAsyncTaskDao;
UpdateChallengeInfosAsyncTask(ChallengeInfosDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(ChallengeInfos... challengeInfos) {
mAsyncTaskDao.insert(challengeInfos[0]);
return null;
}
}
Maybe it's possible with a onPostExecute()
+ Interface
combo like here?
How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?