I am building an Android application with MVVM Architecture. Table A has an Auto-Generated Primary Key column which is a Foreign Key into Table B. When the user clicks a button on the main fragment a row is inserted into Table A. As part of this button's onClickListener, I'd like to retrieve the Auto-Generated Primary Key value (rowId) after creation and insert it into a column in Table B along with more data.
I am using MVVM Architecture, Coroutines, etc... but cannot figure out how to do this. Below is some code and I'm happy to post more if anyone can help.
**// DAO Code**
@Dao
interface WLDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTableA(tableAEntry: TableAEntry) : Long
**// Repo Code (function only)**
suspend fun insertTableA(tableAEntry: TableAEntry): Long {
return WLDAO.insertTableA(TableAEntry)
}
**// View Model Code (function only)**
fun addToTableA(tableAEntry: TableAEntry) = viewModelScope.launch {
repo.insertTableA(TableAEntry)
}