当前位置:网站首页>Use of kotlin collaboration in the project
Use of kotlin collaboration in the project
2022-04-23 18:43:00 【KIDD-4869】
Preface
《 First line of code 》 The third edition believes that many people have read , Xiaobai like me also takes this opportunity to learn Kotlin A wave of , Many things feel “ It's delicious ”! But in terms of collaboration , Encountered some obstacles , The reason is that the complete network request framework may have been encapsulated in the project , Forced by demand , I haven't tried to learn and introduce synergy , Finally, I took the opportunity to meet the needs of my colleagues , Using a co process , The main reason is that it's really convenient to use the collaborative process , And the logic will be smooth . The following is a brief list of their use in the project .
Code section
Let's first look at the encapsulated request , Simplify the request , Go through a process , The use of okhttp.
The demand here is quite wonderful , Two requests were used , But the result was placed in a model Inside , Finally, complete model Send out .
class RequestTest {
suspend fun request() : ListModel{
// perform ,livemodel Wait for the result to return , Execute the next method
val livemodel = getLiveMeetingList()
// perform ,historymodel Wait for the result to return
val historymodel = getHistoryMeetingList()
livemodel.listHistoryData = hothistorymodel.listHistoryData
return livemodel
}
private suspend fun getLiveMeetingList() : ListModel {
return suspendCoroutine {
...
client.newCall(request).enqueue(object : Callback {
override fun onFailure(p0: Call?, p1: IOException?) {
it.resumeWithException(p1)
}
override fun onResponse(p0: Call, p1: Response) {
...
it.resume(listModel)
}
})
}
}
private suspend fun getHistoryMeetingList() : ListModel{
return suspendCoroutine {
... Above ...
}
}
class ListModel {
var listHistoryData
var listLiveData
}
}
It looks a little strange , But change the code , Always fit what you wrote before , The price of re overthrowing is always high .
Next, look at the call place , the reason being that suspend Method , Therefore, execute in the scope of the collaboration , Both methods of opening scope have their disadvantages , Guo Shen also tells the recommended practice in his book .
class TestViewModel : ViewModel() {
private val _data = MutableLiveData<ListModel>()
val data = _data
private val requestTest = RequestTest()
private val job = Job()
private val scope = corouttineScope(job)
fun request() {
scope.launch {
_data.postValue(requestTest.request())
}
}
override fun onCleared() {
super.onCleared()
job?.let {
it.cancel()
}
}
}
Summary
At present, I use this method , Finally, when you don't need to use it cancel It's more convenient . There are many better ways to show this set of , But maybe hard insertion into the project won't work , I wonder if the method provided here is often used by everyone .
版权声明
本文为[KIDD-4869]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603464607.html
边栏推荐
- Introduction to quantexa CDI syneo platform
- 【科普】CRC校验(一)什么是CRC校验?
- ctfshow-web362(SSTI)
- Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
- Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
- [popular science] CRC verification (I) what is CRC verification?
- ESP32 LVGL8. 1 - roller rolling (roller 24)
- 七、DOM(下) - 章节课后练习题及答案
- Esp32 (UART ecoh) - serial port echo worm learning (2)
- 机器学习理论基础篇--关于机器学习的一些术语
猜你喜欢
WiFi ap6212 driver transplantation and debugging analysis technical notes
On iptables
Imx6 debugging LVDS screen technical notes
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
Stm32mp157 wm8960 audio driver debugging notes
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
PyGame tank battle
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
Introduction to quantexa CDI syneo platform
ctfshow-web361(SSTI)
随机推荐
Use of regular expressions in QT
Function recursion and solving interesting problems
Daily CISSP certification common mistakes (April 13, 2022)
CANopen STM32 transplantation
SQL中函数 decode()与 replace()的用法
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
Implementation of TCP UDP communication with golang language
os_ authent_ Prefix
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
程序员如何快速开发高质量的代码?
Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
Daily network security certification test questions (April 12, 2022)
ctfshow-web362(SSTI)
Eight bit binary multiplier VHDL
Ctfshow - web362 (ssti)
配置iptables
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
从技术体系到商业洞察,中小研发团队架构实践之收尾篇
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Get a list of recent apps