Daniel
10/18/2020, 3:37 PMasync
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun <T> CoroutineScope.async(context: CoroutineContext = ..., start: CoroutineStart = ..., block: suspend CoroutineScope.() → TypeVariable(T)): Deferred<TypeVariable(T)> defined in kotlinx.coroutines
I'm not sure how much I can pare down my example, because it seems pretty specific to what I'm trying to do. Here's essentially the function
suspend fun getTile(params: SomeType): GetTileResponse {
val cached = tileCache.get(params)
if (cached != null) {
return GetTileResponse.Available(cached)
}
val request = GetTileRequest(params)
val deferred = async {
makeRemoteRequest()
.map { convertIntoResponse(it) }
}
return GetTileResponse.Loading(deferred)
}
If I ask kotlin to generate an implementation of async, I get something which seems to look very similar to the definition of async (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html)
private fun async(block: suspend CoroutineScope.() -> Either<GetTileError, Bitmap>): Deferred<Either<GetTileError, Bitmap>> {
TODO("Not yet implemented")
}
Adam Powell
10/18/2020, 3:43 PMDaniel
10/18/2020, 3:44 PMDaniel
10/18/2020, 3:44 PMAdam Powell
10/18/2020, 3:45 PMGetTileResponse.Loading
that may or may not be loaded yet. Generally you want a function like this to return the real value after loading is complete, and suspend until there is a result or error to report.Daniel
10/18/2020, 3:45 PMDaniel
10/18/2020, 3:46 PMAdam Powell
10/18/2020, 3:48 PMasync
- you need a CoroutineScope
for it to launch into.Daniel
10/18/2020, 3:48 PMDaniel
10/18/2020, 3:48 PMAdam Powell
10/18/2020, 3:51 PMAdam Powell
10/18/2020, 3:52 PMCoroutineScope
to scope the work of performing data fetches that may be shared across deduped requestsDaniel
10/18/2020, 3:53 PMAdam Powell
10/18/2020, 3:53 PM.await
on a Deferred
from the caching layer, but the Deferred
doesn't escape to the final callerAdam Powell
10/18/2020, 3:53 PM