当前位置:网站首页>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
边栏推荐
- Feature selection feature_ selection--SelectKBest
- QT error: no matching member function for call to ‘connect‘
- Advanced transfer learning
- 视频边框背景如何虚化,简单操作几步实现
- Daily network security certification test questions (April 12, 2022)
- Usage of functions decode() and replace() in SQL
- Ionic 从创建到打包指令集顺序
- Go 语言 GUI 框架 fyne 中文乱码或者不显示的问题
- WiFi ap6212 driver transplantation and debugging analysis technical notes
- CANopen STM32 transplantation
猜你喜欢
Practice of Druid SQL and security in meituan review
Halo open source project learning (VII): caching mechanism
MVVM model
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
Summary of actual business optimization scheme - main directory - continuous update
根据快递单号查询物流查询更新量
Druid SQL和Security在美团点评的实践
STM32: LCD显示
ctfshow-web362(SSTI)
ctfshow-web361(SSTI)
随机推荐
Ctfshow - web362 (ssti)
ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
ESP32 LVGL8. 1 - input devices (input devices 18)
CISSP certified daily knowledge points (April 15, 2022)
Const keyword, variable and function are decorated with const
K210 serial communication
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
Daily CISSP certification common mistakes (April 12, 2022)
PyGame tank battle
Screenshot using projectmediamanager
CISSP certified daily knowledge points (April 13, 2022)
Summary of actual business optimization scheme - main directory - continuous update
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
QT error: no matching member function for call to ‘connect‘
Nacos作为服务注册中心
CANopen usage method and main parameters of object dictionary
机器学习理论基础篇--关于机器学习的一些术语
os_authent_prefix
CISSP certified daily knowledge points (April 19, 2022)
Daily network security certification test questions (April 13, 2022)