当前位置:网站首页>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
边栏推荐
- Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
- Custom prompt box MessageBox in QT
- ESP32 LVGL8. 1 - label (style 14)
- ESP32 LVGL8. 1 - roller rolling (roller 24)
- Druid SQL和Security在美团点评的实践
- RPM包管理
- 机器学习实战 -朴素贝叶斯
- Halo open source project learning (VII): caching mechanism
- 【数学建模】—— 层次分析法(AHP)
- The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
猜你喜欢
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
WiFi ap6212 driver transplantation and debugging analysis technical notes
Teach you to quickly rename folder names in a few simple steps
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
ctfshow-web362(SSTI)
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
How to virtualize the video frame and background is realized in a few simple steps
机器学习实战 -朴素贝叶斯
Chondroitin sulfate in vitreous
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
随机推荐
ESP32 LVGL8. 1 - textarea text area (textarea 26)
Loop path
Daily network security certification test questions (April 15, 2022)
CANopen STM32 transplantation
机器学习理论之(8):模型集成 Ensemble Learning
MVVM model
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
PyGame tank battle
纠结
CISSP certified daily knowledge points (April 13, 2022)
Introduction to ROS learning notes (II)
ESP32 LVGL8. 1 - input devices (input devices 18)
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
STM32: LCD显示
关于unity文件读取的操作(一)
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
Usage of functions decode() and replace() in SQL
玻璃体中的硫酸软骨素
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
QT error: no matching member function for call to ‘connect‘