Мне интересно, есть ли способ дождаться завершения потока и вернуть результат внутри функции приостановки. transactionRepository.getAll(accountId)возвращает поток транзакций
override suspend fun getAccount(accountId: Int): Account {
val account = Account(accountRepository.get(accountId))
transactionRepository.getAll(accountId).mapIterable {
val transaction = Transaction(it)
val category = Category(categoryRepository.get(it.categoryId))
transaction.category = category
transaction.account = account
return@mapIterable transaction
}.collect {
account.transactions = it
}
//TODO: How can i return an account after the flow has been executed?
}
Функция getAll определена в моем репозитории:
fun getAll(accountId: Int): Flow<List<DatabaseTransaction>>
Решение проблемы
Предполагая, что вам нужно только первое значение, возвращаемое потоком, и это List<Transaction>, вы можете использовать first(). Но я только предполагаю, потому что я не знаю, что getAll()возвращает, и я не знаком с mapIterable.
override suspend fun getAccount(accountId: Int): Account {
val account = Account(accountRepository.get(accountId))
val transactionsFlow: Flow<List<Transaction>> = transactionRepository.getAll(accountId).mapIterable {
val transaction = Transaction(it)
val category = Category(categoryRepository.get(it.categoryId))
transaction.category = category
transaction.account = account
return@mapIterable transaction
}
account.transactions = transactionsFlow.first()
return account
}
Комментариев нет:
Отправить комментарий