Last active
October 29, 2018 20:36
-
-
Save zamahaka/10c90a184d69d086e35ad7af49996b84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val balance = userHandler.user | |
.switchMap { user -> | |
Log.d("myLog", "switchMap: $user") | |
pusherManager.getUpdateBalancesLiveData(user) | |
} | |
.mapSuspended(context = BackgroundDispatcher) { unit -> | |
Log.d("myLog", "gettingBalance") | |
val balance = getBalance() // suspend fun | |
balance | |
} | |
inline fun <T : Any, R : Any> LiveData<T>.switchMap( | |
crossinline transform: (T) -> LiveData<R> | |
): LiveData<R> = Transformations.switchMap(this) { it?.let(transform) } | |
inline fun <T : Any, R : Any> LiveData<T>.mapSuspended( | |
context: CoroutineContext, | |
crossinline transform: suspend (T) -> R | |
): LiveData<R> = object : MediatorLiveData<R>() { | |
private var job: Job? = null | |
set (value) { | |
field?.cancel() | |
field = value | |
} | |
init { | |
addSource(this@mapSuspended) { t -> | |
job = launch(context = context) { postValue(transform(t)) } | |
} | |
} | |
} |
But when I remove inline everything works
Coroutines version: 0.24.0, Kotlin 1.2.71
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crash on class cast exception
E/AndroidRuntime: FATAL EXCEPTION: ForkJoinPool.commonPool-worker-3
Process: myapp.debug, PID: 2942
java.lang.ClassCastException: kotlin.coroutines.experimental.intrinsics.CoroutineSuspendedMarker cannot be cast to myapp.core.Result
at myapp.domain.provider.WalletBalanceProvider$$special$$inlined$mapSuspended$1$1$1.doResume(LifecycleExtensions.kt:50) // line 29 above, postValue imho
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:150)
at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:14)
at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:285)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1152)
at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1990)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1938)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)