欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android結(jié)合kotlin使用coroutine的方法實(shí)例

 更新時(shí)間:2020年12月01日 15:10:58   作者:Jiajun的編程隨想  
這篇文章主要給大家介紹了關(guān)于Android結(jié)合kotlin使用coroutine的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

最近入了Android坑,目前還處于瘋狂學(xué)習(xí)的狀態(tài),所以很久都沒(méi)有寫博客了。今天記錄一個(gè)小代碼片段,在Android上使用coroutine 的小例子。

由于我自己是做一個(gè)記賬軟件來(lái)學(xué)習(xí)的,我用了gRPC,最開(kāi)始我是使用線程來(lái)做網(wǎng)絡(luò)請(qǐng)求的:

thread {
 // 網(wǎng)絡(luò)請(qǐng)求代碼

 runOnUiThread {
  // 更新UI的代碼
 }
}

今天把這一套全部重寫成用coroutine。

首先coroutine得有個(gè)調(diào)度器,英文叫做 “Dispatchers”,有這么幾個(gè):

  • Dispatchers.Main 這里面的coroutine跑在主線程上,在Android里也就是UI線程,所以如果在這里面的coroutine也執(zhí)行大量耗時(shí)代碼的話,也是會(huì)卡UI的
  • Dispatchers.IO 用來(lái)跑大IO的
  • Dispatchers.Default 用來(lái)跑高CPU消耗的
  • Dispatchers.Unconfined 不綁定在任何特定執(zhí)行線程上

然后,為了多個(gè)coroutine之間可以分組啊,就像進(jìn)程里可以放很多線程那樣,又搞了一個(gè)概念,叫做 scope,默認(rèn)有一個(gè)全局scope,叫做 GlobalScope,全局的, 就和全局變量一樣,在Android上,這個(gè)里面跑的coroutine,生命周期和app一樣久,不推薦在這里起coroutine。

推薦的方式是每個(gè)Activity里起一個(gè)scope,然后再launch。

所以我就這樣寫基類:

abstract class BaseActivity : AppCompatActivity(), CoroutineScope {
 /*
 默認(rèn)的coroutine scope是Main,也就是UI線程(主線程)。如果要做IO,比如網(wǎng)絡(luò)請(qǐng)求,記得
 包裹在 launch(Dispatchers.IO) {} 里,如果要大量計(jì)算,包裹在 launch(Dispatcher.Default) {} 里
 或者直接寫 launch。 UI操作則用 withContext(Dispatchers.Main) {} 切回來(lái)
  */
 private val job = SupervisorJob()
 override val coroutineContext: CoroutineContext
  get() = Dispatchers.Main + job

 override fun onDestroy() {
  super.onDestroy()
  coroutineContext.cancelChildren()
 }

這樣子之后,就可以直接launch,起coroutine了:

launch {
 val req = CreateFeedbackReq.newBuilder().build()
 val respAny = callRPC {
  api.createFeedback(req)
 }
 respAny?:return@launch

 val resp = respAny as CreateFeedbackResp
 if (handleRespAction(resp.action)) {
  withContext(Dispatchers.Main) {
   showSnackBar(R.string.thank_you_for_feedback)
   delay(1000)
   finish()
  }
 }
}

如上,默認(rèn)情況下,root coroutine就是當(dāng)前所在activity,而他們默認(rèn)會(huì)在 Dispatchers.Main 上執(zhí)行,如果想要coroutine在 別的 dispatcher 上執(zhí)行,就用 withContext,然后里面如果又想更新UI的話,就用 withContext(Dispatchers.Main)。

那為啥 launch 不傳參數(shù)的話,就是直接用的 Dispatchers.Main 呢?因?yàn)槠鋵?shí) CoroutineScope 是一個(gè)接口,而 coroutineContext 是里面的一個(gè)變量:

public interface CoroutineScope {
 /**
  * The context of this scope.
  * Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope.
  * Accessing this property in general code is not recommended for any purposes except accessing the [Job] instance for advanced usages.
  *
  * By convention, should contain an instance of a [job][Job] to enforce structured concurrency.
  */
 public val coroutineContext: CoroutineContext
}

我們?cè)賮?lái)看看 launch 的實(shí)現(xiàn):

public 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
}

@ExperimentalCoroutinesApi
public actual 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
}

可以看到,默認(rèn)情況下,會(huì)把當(dāng)前的 coroutineContext 放在前面。

Kotlin的coroutine很好用,不過(guò)我感覺(jué)還是有點(diǎn)復(fù)雜,我也還在學(xué)習(xí)。

ref:

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/index.html

到此這篇關(guān)于Android結(jié)合kotlin使用coroutine的文章就介紹到這了,更多相關(guān)Android結(jié)合kotlin使用coroutine內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論