tags: Coroutine customize android rxjava kotlin
Coroutines is a lighter than a thread. Just as one process can have multiple threads, one thread can have multiple coroutines
One thing that must be clear is that the operation of multiple corporations of a thread is serial. If it is a multi -core CPU, multiple processes or multiple threads in one process can run parallel, but an intra -threaded coroutine is It is definitely serial, no matter how many cores of the CPU have. Although the coroutine is a special function after all, it is still a function. Multiple functions can be run in a thread, but these functions are running serialized.
When an coroutine runs, other coroutines must be hung.
The biggest feature of processing multi -task concurrency is that it can automatically help us switch threads. Simply put, corporate is a thread framework

Council and thread can be used to achieve asynchronous calls, but there are essential differences between the two
What are the advantages of corporate compared to RXJAVA?
Rxjava is more powerful than corporate?
mViewModel.getOrder(uuid).flatMap {
mViewModel.queryClient(QueryRequest().apply {
start = 0
limit = 20
filters.add(FilterParam("keyword:%=%", it.data?.customerName))
})
}.flatMap {
if (it.data?.size == 0) {
throw Throwable("Throwing abnormal here")
} else {
setCurClientData(ChooseClientWrapper(it.data?.get(0)))
}
mViewModel.queryGoods(QueryRequest().apply {
filters.add(FilterParam("shopId:=", StorageMgr.getConfig(Constants.PS4_WosShop)))
filters.add(FilterParam("customerId:=", mViewModel.curClient.get().data?.code))
filters.add(FilterParam("wrhId:=", mViewModel.curWrh.get().data?.uuid))
filters.add(FilterParam("skuId:in", arrayListOf<String>().apply {
mViewModel.goodsLines.forEach { goodsLine ->
add(goodsLine.gdGid.toString())
}
}))
})
}
.doOnSubscribe { showLoading() }
.doAfterTerminate { hideLoading() }
.bindLifeCycle(this)
.subscribe({},{})
mViewModel.listPictureParam.clear()
Observable.fromIterable(mViewModel.listPictureCompress).concatMap {
mViewModel.uploadImage(BOssImage().apply {
name = FileUtils.getFileNameWithSuffix(it)
ext = FileUtils.getSuffix(it)
bytes = Base64.encodeToString(FileUtils.file2byte(it), Base64.DEFAULT)
}).toObservable()
}.map {
mViewModel.listPictureParam.add(it.data?.url)
}.count().flatMap {
mViewModel.deliver(RfOrderDeliverReq().apply {
orderId = mViewModel.uuid.get()
if (mViewModel.carNumEdit.get() != null) {
carNumber = mViewModel.carNumEdit.get().trim()
}
if (mViewModel.remarkEdit.get() != null) {
remark = mViewModel.remarkEdit.get().trim()
}
vouchers.addAll(mViewModel.listPictureParam.map { it })
})
}.doOnSubscribe { showLoading() }
.doAfterTerminate { hideLoading() }
.bindLifeCycle(this)
.subscribe({},{})
// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
CoroutineScope(Dispatchers.Main).launch{
}
It involves 2 new things here, the first issuspend The modification character, the other iswithContext function
The role of the Suspend is that the logo method is the function of hanging the function, which is modified as a function of the hanging function.Can only be called in the coroutine or other hanging functions
The role of the WithContext function is to switch threads. You can see Dispatches.io later, that is, I switched to the IO thread
In this way, the hung function can be cut to other threads to execute, and after the execution, the main thread that returns to Launch continues to execute, and the effect of automatic switching thread
private suspend fun getData(): String {
return withContext(Dispatchers.IO) {
"hen_coder"
}
}
What is a coroutine Coroutine is a set of thread-encapsulated API provided by Kotlin In Kotlin, a typical use scenario for coroutines is thread control What's so good about the coroutine In Kotlin, wh...
I started to learn things related to cooperation in summer vacation. At the beginning, I looked confused, and then I repeated the cycle of understanding -> eh what is going on -> look again -...
Probe into Kotlin Coroutine (1) 2017, Google announcedKotlin Become an official development language of Android and adds support for COROUTINE (equation, simply as a lightweight thread) at version 1.1...
Last week we talked about Kotlin Coroutine's basic API and gave some simple packages. I don’t want to give it too much. Just in the 1.1 Beta 2 released a few days ago, all the coroutine API pack...
Series e -book:Portal Why learn Kotlin coroutines? We already have a very complete type of JVM library, such as RXJAVA and Reactor. In addition, Java itself supports multi -threaded threads, and most ...
Kotlin Coroutines (Coroutine) Completely Analytical Series: Kotlin Coroutines (coroutine) complete analysis (a), introduction to the coroutine Kotlin Coroutines (coroutine) complete parsing (2), deep ...
Foreword Kotlin is now a fresher language. I have asked friends around me, and some seem to start using them to start writing backgrounds, and some start to refactor Android project code with kotlin. ...
Background Kotlinx.coroutines is a feature-rich coroutine library developed by JetBrains. It contains many of the primitives that enable advanced coroutines covered in this guide, including launch, as...
First, access Second, the way of use Commonly used ways: CoroutineScope.launch() CoroutineScope.async() Third, about the exemplary example JOB is used to handle the level. For each created sweeper (vi...
Channel channel of the coroutine in Kotlin (1) Channel-channel Channel capacity Iterate Channel Produce and Actor Channel's closure BroadcastChannel Channel-channel Channel is actually a secure queue,...