Open mustafakuloglu opened 6 years ago
I made a fork of this project and there is an example of using ListAdapter: https://github.com/vladsonkin/bikeindex/blob/master/app/src/main/kotlin/com/sonkins/bikeindex/features/bikes/filter/colors/ColorsAdapter.kt
Will submit a PR for this
You can do something like below i think.
abstract class UseCase<out Type, in Params> : CoroutineScope where Type : Any {
private val job = Job()
abstract suspend fun run(params: Params): Either<Failure, Type>
fun execute(onResult: (Either<Failure, Type>) -> Unit, params: Params) {
val job = async(CommonPool) { run(params) }
launch { onResult.invoke(job.await()) }
}
fun cancel() = job.cancel()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
}
@Toffor is it possible to use launch
instead of async
?
@Zhuinden I think its possible but in this example if you don't use async it will throw NetworkOnMainThreadException. If you use https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter or a solution like this, then you can use just launch because network request already done an other context. Also I think suspend modifier is redundant in this example.
Here is some explanation about why we must use ListAdapter.
This project is so good. Thanks for this project and explanations. But if you use diffutil in adapter, you can get some good skills. Recycler.Adapter and diffutil is little bit complex and have some boilerplate code. ListAdapter have some good solutions about this. Details in link. Please read that article.