【发布时间】:2019-02-21 21:08:36
【问题描述】:
我有一个方法叫saveAccount
fun saveAccount(id: Int, newName: String): Account {
val encryptedNames: List<String> = repository.findNamesById(id)
val decryptedNames: List<String> = encryptedNames.map { cryptographyService.decrypt(it) }
if(decryptedNames.contains(newName))
throw IllegalStateException()
return repository.save(newName)
}
我想同时解密所有的名字,所以我做了:
suspend fun saveAccount(id: Int, newName: String): Account {
val encryptedNames: List<String> = repository.findNamesById(id)
val decryptedNames: List<String> = encryptedNames.map {
CoroutineScope(Dispatchers.IO).async {
cryptographyService.decrypt(it)
}
}.awaitAll()
if(decryptedNames.contains(newName))
throw IllegalStateException()
return repository.save(newName)
}
到目前为止一切都很好,但问题是:我不能让saveAccount 成为挂起函数。我该怎么办?
【问题讨论】: