当前位置:网站首页>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