Brian Dilley
03/23/2021, 5:49 PMtrue
result that it finds):
override fun emailExists(email: String): Boolean {
val results = shards.asyncAll { userDao.emailExists(email) }
return runBlocking {
results.map { it.await() }
.firstOrNull { it }
} ?: false
}
the shards.asyncAll method is:
fun <T> async(
shardId: Long,
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T): Deferred<T> {
return scope.async(context, start) {
selectShard(shardId)
block()
}
}
fun <T> asyncAll(
shardIds: Collection<Long> = this.shardIds,
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T): List<Deferred<T>> {
return shardIds.map { async(it, context, start, block) }
}
So first question is if that code above (emailExists
) will work, and the second question is: how can i change it so that the first Deferred<Boolean>
result that returns that is true
returns from the method (rather than it testing them in order as they come back - I’d like it so that the very first one that comes back with a result of true
triggers a return)marstran
03/24/2021, 8:02 AMshards.asFlow()
.map { userDao.emailExists(it) }
.firstOrNull { it }
With just a list, you will always trigger all of the emailExists
calls.Brian Dilley
03/24/2021, 3:12 PMBrian Dilley
03/24/2021, 3:13 PMtrue
is returned.marstran
03/24/2021, 4:15 PMCoroutineScope
to control that.marstran
03/24/2021, 4:15 PMasync
coming from?Brian Dilley
03/24/2021, 4:55 PM