Share
Embed
Coroutine-first Android Architecture
Coroutines allow us to write asynchronous code in an easy-to-read, declarative style. The structured concurrency pattern allows us to write this code without fear of leaks. But there’s much more to the coroutines API than suspend modifiers and CoroutineScopes, and with these new abilities should come new patterns.
In this talk, we’ll discuss the power behind structured concurrency and how we can use it to make our entire stack lifecycle-aware. We’ll see how we can use the tools coroutines give us to inform our application architecture, so that we can quickly write maintainable and testable features at a large scale.
Rick Busarow
July 17, 2019
Transcript
122 slides
-
class SomeViewModel(private val repository: SomeRepository) { private val _liveData =
MutableLiveData<String>() val liveData: LiveData<String> = _liveData } -
class SomeViewModel(private val repository: SomeRepository) { private val _liveData =
MutableLiveData<String>().apply { value = repository.getTheWindsOfWinter() } val liveData: LiveData<String> = _liveData } -
class SomeScreen : LifecycleOwner { val viewModel: SomeViewModel by lazy
{ … } fun onCreate() { viewModel.liveData.observe(this, Observer { str !-> tvSomeTextView.text = str }) } } -
class SomeRepository(val api: Api) { fun getTheWindsOfWinter(): String { Thread.sleep(LONG_TIME)
return api.getTwow() } } -
class SomeRepository(val api: Api) { fun getTheWindsOfWinter(): LiveData<String> { return
MutableLiveData<String>().apply { Thread.sleep(LONG_TIME) value = api.getTwow() } } } -
class SomeViewModel(private val repository: SomeRepository) { private val _liveData =
MutableLiveData<String>().apply { value = repository.getTheWindsOfWinter() } val liveData: LiveData<String> = _liveData } -
class SomeViewModel(private val repository: SomeRepository) { val liveData: LiveData<String> =
repository.getTheWindsOfWinter() } -
class SomeRepository(val api: Api) { fun getTheWindsOfWinter(): LiveData<String> { return
MutableLiveData<String>().apply { Thread.sleep(LONG_TIME) value = api.getTwow() } } } -
class SomeRepository(val api: Api) { fun getTheWindsOfWinter(): LiveData<String> { return
MutableLiveData<String>().apply { api.getTwow { value = it } } } } -
class SomeRepository(val api: Api) { fun getTheWindsOfWinter(): LiveData<String> { return
MutableLiveData<String>().apply { api.getTwow { value = it } } } } -
class SomeRepository(val api: Api) { fun getTheWindsOfWinter(): LiveData<String> { return
MutableLiveData<String>().apply { api.getTwow { postValue(it) } } } } -
class SomeViewModel(private val repository: SomeRepository) { val liveData: LiveData<String> =
repository.getTheWindsOfWinter() } -
class SomeViewModel(private val repository: SomeRepository) { val liveData: LiveData<List<String!>> =
Transformations.map(repository.getTheWindsOfWinter()) { novel !-> novel!!!.split("half a hundred") } } -
class SomeViewModel(private val repository: SomeRepository) { val liveData: LiveData<List<String!>> =
Transformations.map(repository.getTheWindsOfWinter()) { novel !-> novel!!!.split("half a hundred") } } -
class SomeViewModel( private val repository: SomeRepository, private val coroutineScope: CoroutineScope
) { private val _liveData = MutableLiveData<List<String!>>() val liveData: LiveData<List<String!>> = _liveData val transform: LiveData<Unit> = Transformations.map(repository.getTheWindsOfWinter()) { novel !-> coroutineScope.launch { _liveData.postValue(novel!!!.split("half a hundred”)) } } } -
class SomeViewModel( private val repository: SomeRepository, private val coroutineScope: CoroutineScope
) { private val _liveData = MutableLiveData<List<String!>>() val liveData: LiveData<List<String!>> = _liveData val transform: LiveData<Unit> = Transformations.map(repository.getTheWindsOfWinter()) { novel !-> coroutineScope.launch { _liveData.postValue(novel!!!.split("half a hundred”)) } } } -
class SomeScreen : LifecycleOwner { val viewModel: SomeViewModel by lazy
{ … } fun onCreate() { viewModel.liveData.observe(this, Observer { str !-> tvSomeTextView.text = str }) } } -
class SomeScreen : LifecycleOwner { val viewModel: SomeViewModel by lazy
{ … } fun onCreate() { viewModel.transform.observe(this, Observer { }) viewModel.liveData.observe(this, Observer { str !-> tvSomeTextView.text = str }) } } -
class SomeViewModel( private val repository: SomeRepository, private val coroutineScope: CoroutineScope
) { val liveData: LiveData<List<String!>> = Transformations.switchMap( repository.getTheWindsOfWinter() ) { full !-> liveData(Default) { val split = full.split("half a hundred") emit(split) } } } lifecycle-livedata-ktx:2.2.0-alpha02 -
class SomeViewModel( private val repository: SomeRepository, private val coroutineScope: CoroutineScope
) { val liveData: LiveData<List<String!>> = Transformations.switchMap( repository.getTheWindsOfWinter() ) { full !-> liveData(Default) { val split = full.split("half a hundred") emit(split) } } } lifecycle-livedata-ktx:2.2.0-alpha02 -
class SomeViewModel( private val repository: SomeRepository, private val coroutineScope: CoroutineScope
) { val liveData: LiveData<List<String!>> = Transformations.switchMap( repository.getTheWindsOfWinter() ) { full !-> liveData(Default) { val split = full.split("half a hundred") emit(split) } } } lifecycle-livedata-ktx:2.2.0-alpha02 -
class SomeViewModel( private val repository: SomeRepository, private val coroutineScope: CoroutineScope
) { val liveData: LiveData<List<String!>> = repository.getTheWindsOfWinter().switchMap { full !-> liveData(Default) { val split = full.split("half a hundred") emit(split) } } } lifecycle-livedata-ktx:2.2.0-alpha02 -
LiveData ‣ Always tied to the main thread ‣ Requires
LifecycleOwner ‣ Observer-heavy ‣ Written by George R. R. Martin -
fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() !-> Unit ): Job { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyStandaloneCoroutine(newContext, block) else StandaloneCoroutine(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine } -
fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() !-> Unit ): Job { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyStandaloneCoroutine(newContext, block) else StandaloneCoroutine(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine } -
fun CoroutineScope.newCoroutineContext( context: CoroutineContext ): CoroutineContext { val combined =
coroutineContext + context val debug = if (DEBUG) combined + CoroutineId(COROUTINE_ID.incrementAndGet()) else combined return if ( combined !!!== Dispatchers.Default !&& combined[ContinuationInterceptor] !== null ) debug + Dispatchers.Default else debug } -
fun CoroutineScope.newCoroutineContext( context: CoroutineContext ): CoroutineContext { val combined =
coroutineContext + context val debug = if (DEBUG) combined + CoroutineId(COROUTINE_ID.incrementAndGet()) else combined return if ( combined !!!== Dispatchers.Default !&& combined[ContinuationInterceptor] !== null ) debug + Dispatchers.Default else debug } -
suspend fun <T> withContext( context: CoroutineContext, block: suspend CoroutineScope.() !->
T ): T = suspendCoroutineUninterceptedOrReturn sc@ { continuation !-> val oldContext = continuation.context … } -
suspend fun <T> withContext( context: CoroutineContext, block: suspend CoroutineScope.() !->
T ): T = suspendCoroutineUninterceptedOrReturn sc@ { continuation !-> val oldContext = continuation.context … } -
CoroutineScope CoroutineContext interface CoroutineContext { val job: Job val continuationInterceptor:
ContinuationInterceptor val coroutineExceptionHandler: CoroutineExceptionHandler val coroutineName: CoroutineName } -
CoroutineScope CoroutineContext interface CoroutineContext { val job: Job val continuationInterceptor:
ContinuationInterceptor val coroutineExceptionHandler: CoroutineExceptionHandler val coroutineName: CoroutineName } -
class SomeClass : CoroutineScope { val A = Job() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class SomeClass : CoroutineScope { val A = Job() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class SomeClass : CoroutineScope { val A = Job() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class SomeClass : CoroutineScope { val A = Job() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class SomeClass : CoroutineScope { val A = Job() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class SomeClass : CoroutineScope { val A = SupervisorJob() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class SomeClass : CoroutineScope { val A = SupervisorJob() override
val coroutineContext = A fun someFunction() { val B = launch { val D = launch {} val E = launch {} } val C = launch { val F = launch {} } } } -
class FileParser(file: File) : CoroutineScope { override val coroutineContext =
Job() + Dispatchers.IO suspend fun parse(): String = TODO() } -
class FileParser(file: File) : CoroutineScope { override val coroutineContext =
Job() + Dispatchers.IO suspend fun parse(): String = TODO() } class SomeOtherClass { var novel: String? = null suspend fun onUpdatedFile(file: File) { novel = FileParser(file).parse() } } -
class FileParser(file: File) : CoroutineScope { override val coroutineContext =
Job() + Dispatchers.IO suspend fun parse(): String = TODO() } class SomeOtherClass { var novel: String? = null suspend fun onUpdatedFile(file: File) { FileParser(file).launch { … } } } -
class FileParser(file: File) { private val scope = CoroutineScope(Job() +
Dispatchers.IO) suspend fun parse(): String = TODO() } class SomeOtherClass { var novel: String? = null suspend fun onUpdatedFile(file: File) { FileParser(file).launch { … } } } -
fun SupervisorScope( continuationInterceptor: ContinuationInterceptor = Dispatchers.Default ): CoroutineScope = CoroutineScope(SupervisorJob()
+ continuationInterceptor) fun DefaultCoroutineScope() = SupervisorScope(Dispatchers.Default) fun IOCoroutineScope() = SupervisorScope(Dispatchers.IO) -
Channel ‣ Observable type ‣ Linked blocking queue ‣ Thread
Concurrency-safe ‣ Synchronization primitive ‣ Sender and receiver have their own CoroutineScope -
suspend fun talkToYourself() { val channel = Channel<Int>() scope.launch {
repeat(5) { channel.send(it) } } val output = mutableListOf<Int>() for (i in channel) { output.add(i) } output shouldBe listOf(0, 1, 2, 3, 5) } -
suspend fun talkToYourself() { val channel = Channel<Int>() scope.launch {
repeat(5) { channel.send(it) } } val output = mutableListOf<Int>() for (i in channel) { output.add(i) } output shouldBe listOf(0, 1, 2, 3, 5) } -
suspend fun talkToYourself() { val channel = Channel<Int>() scope.launch {
repeat(5) { channel.send(it) } } val output = mutableListOf<Int>() for (i in channel) { output.add(i) } output shouldBe listOf(0, 1, 2, 3, 5) } -
suspend fun talkToYourself() { val channel = Channel<Int>() scope.launch {
repeat(5) { channel.send(it) } } val output = mutableListOf<Int>() for (i in channel) { output.add(i) } output shouldBe listOf(0, 1, 2, 3, 5) } -
suspend fun talkToYourself() { val channel = Channel<Int>() scope.launch {
repeat(5) { channel.send(it) } } val output = mutableListOf<Int>() for (i in channel) { output.add(i) } output shouldBe listOf(0, 1, 2, 3, 5) } ORDER OF OPERATIONS Send 0 Receive 0 Send 1 Receive 1 … -
class SomeClass { private val _channel = Channel<Int>() val channel:
ReceiveChannel<Int> = _channel } -
fun <T> ReceiveChannel<T>.observe( coroutineScope: CoroutineScope, observer: CoroutineScope.(t: T) !-> Unit
): Job = coroutineScope.launch { for (t in this@observe) { observer(t) } } -
fun <T> ReceiveChannel<T>.observe( coroutineScope: CoroutineScope, observer: CoroutineScope.(t: T) !-> Unit
): Job = coroutineScope.launch { for (t in this@observe) { observer(t) } } -
fun <T> ReceiveChannel<T>.observe( coroutineScope: CoroutineScope, observer: CoroutineScope.(t: T) !-> Unit
): Job = coroutineScope.launch { for (t in this@observe) { observer(t) } } -
fun <T> ReceiveChannel<T>.observe( coroutineScope: CoroutineScope, observer: CoroutineScope.(t: T) !-> Unit
): Job = coroutineScope.launch { for (t in this@observe) { observer(t) } } -
class SomeClass( val someRepository: SomeRepository, val scope: CoroutineScope ) {
init { someRepository.channel.observe(scope) { emitted !-> println(emitted) } } } -
class SomeClass( val someRepository: SomeRepository, val scope: CoroutineScope ) {
init { someRepository.channel.observe(scope) { emitted !-> println(emitted) } } } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun Screen.whileResumed(block: CoroutineScope.() !-> Job) { var job: Job? =
null val observer = object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun onResume() { job = block() } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { job!?.cancel() } } lifecycle.addObserver(observer) } -
fun getUserProfile(): Deferred<UserProfile?> { val deferred = CompletableDeferred<UserProfile?>() api.getUserProfile() .enqueue(object
: Callback<UserProfile> { override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) { deferred.complete(response.body()) } override fun onFailure(call: Call<UserProfile>, t: Throwable) { deferred.complete(null) } }) return deferred } -
fun getUserProfile(): Deferred<UserProfile?> { val deferred = CompletableDeferred<UserProfile?>() api.getUserProfile() .enqueue(object
: Callback<UserProfile> { override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) { deferred.complete(response.body()) } override fun onFailure(call: Call<UserProfile>, t: Throwable) { deferred.complete(null) } }) return deferred } -
fun getUserProfile(): Deferred<UserProfile?> { val deferred = CompletableDeferred<UserProfile?>() api.getUserProfile() .enqueue(object
: Callback<UserProfile> { override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) { deferred.complete(response.body()) } override fun onFailure(call: Call<UserProfile>, t: Throwable) { deferred.complete(null) } }) return deferred } -
fun getUserProfile(): Deferred<UserProfile?> { val deferred = CompletableDeferred<UserProfile?>() api.getUserProfile() .enqueue(object
: Callback<UserProfile> { override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) { deferred.complete(response.body()) } override fun onFailure(call: Call<UserProfile>, t: Throwable) { deferred.complete(null) } }) return deferred } -
fun getUserProfile(): Deferred<UserProfile?> { val deferred = CompletableDeferred<UserProfile?>() api.getUserProfile() .enqueue(object
: Callback<UserProfile> { override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) { deferred.complete(response.body()) } override fun onFailure(call: Call<UserProfile>, t: Throwable) { deferred.complete(null) } }) return deferred } -
class SomeClass(val repository: ProfileRepository) { var profile: UserProfile? = null
suspend fun updateProfile() { profile = repository.getUserProfile().await() } } -
class SomeClass(val repository: ProfileRepository) { var profile: UserProfile? = null
suspend fun updateProfile() { profile = repository.getUserProfile().await() } }