I have a function like this that inserts a new row in a table with rowId:
@Composable
fun AddNewCustomer() {
val db = CustomersDatabase.getDatabase(LocalContext.current)
val coroutineScope = rememberCoroutineScope()
val createMainEntity = {
coroutineScope.launch(Dispatchers.IO) {
val rowId = db.customersDao().getLast()!!.id + 1
db.customersDao().insertPrimaries(CustomersEntity(rowId, null, null, null))
}
}
createMainEntity()
otherFunc(db, coroutineScope)
}
and in another function, I insert a new row to another table:
@Composable
private fun otherFunc(
db: CustomersDatabase,
coroutineScope: CoroutineScope,
) {
val save = {
coroutineScope.launch(Dispatchers.IO) {
delay(100)
val rowId = db.customersDao().getLast()!!.id
db.customersDao().insertPhone(PhoneNumberEntity(phone = "", customerId = rowId , field = "" ))
}
}
save()
}
I want to save() in otherFunc
waits till createMainEntity finishes,
with delay, I can be sure the createMainEntity
finishes first but it's a dirty way how can I do that better.