Share
Embed
Transcript
29 slides
-
Hi, I’m Adora Software Engineer Creator of AdoraHack Women Techmakers
Ajah Ambassador FSDLagos & Android Ngr Co-organizer Twitter, @theadoranwodo -
What we want to do val order = fetchOrderDetails() textView.text
= order.code Never block the main thread -
The Reality thread{ val order = fetchOrderDetails() textView.text = order.code
// CalledFromWrongThreadException } -
A Solution fetchOrderDetails{ order -> textView.text = order.code } Lots
of memory leaks Also very difficult to read. We don’t want callback hell :( -
A Nightmare fetchOrderDetails{ order -> textView.text = order.code someDbTransaction{ db_res
-> anotherCallback{ res -> saveToFile{ res -> // and it continues ... } } } } -
Let’s try Coroutines Coroutines are lightweight threads. Anti callbacks. Write
code sequentially. Makes code easier to read. We can have much more coroutines as opposed to threads. Coroutine mechanism: suspend and resume. Declare a very heavy task as a suspend function -
Let’s try Coroutines - suspend function suspend fun fetchOrderDetails(): Order
{ … } suspend fun doSomething() { val order = fetchOrderDetails() textView.text = order.code } -
Where can I call a suspend function? In another suspend
function’ In coroutine builders (e,g, async, launch) In suspend lambdas In coroutine scopes -
WorkManager WorkManager provides a unified API for background work taking
battery and compatibility across Android versions to account. - Worker - CoroutineWorker - RxWorker - ListenableWorker -
WorkManager & Coroutines dependencies { def work_version = "2.0.1" //
Kotlin + coroutines implementation "androidx.work:work-runtime-ktx:$work_version" } Snippet from: https://developer.android.com/topic/libraries/architecture/workmanager/advanced/coroutineworker.html -
WorkManager & Coroutines class CoroutineDownloadWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context,
params) { override val coroutineContext = Dispatchers.IO override suspend fun doWork(): Result = coroutineScope { val jobs = (0 until 100).map { async { downloadSynchronously("https://www.google.com") } } // awaitAll will throw an exception if a download fails, which CoroutineWorker will treat as a failure jobs.awaitAll() Result.success() } } Snippet from: https://developer.android.com/topic/libraries/architecture/workmanager/advanced/coroutineworker.html -
Coroutine leaks To deal with Coroutine leaks, Kotlin introduced Coroutine
scopes. A scope is a way to keep track of coroutines. Coroutines run in a scope and the scope has the ability to cancel all the coroutines inside it. Use scopes to avoid leaks. Structured concurrency. -
Coroutine Scopes //viewmodel scope implementation “androidx.lifecycle:lifecycle-viewmodel-ktx:$view_model_scope_version” //lifecycle scope implementation “androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_scope_version”
//livedata implementation “androidx.lifecycle:lifecycle-livedata-ktx:$livedata_scope_version” -
viewModelScope Coroutines are useful here when there is work that
should be done only when the ViewModel is active. class MyViewModel: ViewModel() { init { viewModelScope.launch { // Coroutine that will be canceled when the ViewModel is cleared. } } } -
LifecycleScope A LifecycleScope is defined for each Lifecycle object. Any
coroutine launched in this scope is canceled when the Lifecycle is destroyed. You can access the Lifecycle’s CoroutineScope either via lifecycle.coroutineScope or lifecycleOwner.lifecycleScope properties. LifecycleOwner could be an Activity or a Fragment. -
liveData LiveData is an observable value holder for UI It
is now interoperable with Coroutines Compute value in Coroutine and serve as LiveData -
liveData val user = liveData { val data = database.loadUser()
// loadUser is a suspend function. emit(data) } - Use the liveData builder function to call a suspend function asynchronously, - use emit() to emit the result: -
Testing Coroutines kotlinx-coroutines-test : Experimental coroutines API It’s not coupled
to any testing libraries, you can use any library. -
Testing Coroutines val testDispatcher = TestCoroutineDispatcher() val testScope = TestCoroutineScope(testDispatcher)
@Before fun setup(){ Dispatchers.setMain(testDispatcher) } @After fun tearDown(){ Dispatchers.resetMain() testScope.cleanupTestCoroutines() } -
Testing Coroutines @get: Rule val testCoroutineRule = TestCoroutineRule() @Test fun
testSomething() = testCoroutineRule.runBlockingTest { // test code here }