Android : Kotlin Retrofit 동기 처리
2022. 11. 2. 22:50ㆍ[Android APP] feat. Kotlin/Kotlin 공부
문제 상황
for문으로 retrofit을 호출하는데 0~5까지 Request 파라미터를 넣는 상황에서 RecyclerView에 item이 순서대로 들어가지 않고 랜덤으로 (0, 1, 2, 3, 4, 5가 아니라 1, 2, 0, 5, 4)이런 식으로 들어가서 골치아픈 일이 있었다.
원인
Retrofit 라이브러리는 "비동기식"으로 처리되기 때문에 순차적으로 호출되는게 아니라 메모리에 따라서 먼저 호출될수도, 나중에 호출될 수도 있다.
따라서 Thread나 Coroutine을 사용해 동기식 처리 방식으로 바꿔주어 순서대로 RecyclerView에 item을 넣어줄 수 있다.
for(jobtype in 0..5)
{
Log.d("Retro", "${jobtype+1}번 째 루프 도는중..")
workforce(jobtype)
}
fun workforce(jobtype:Int) {
workforce_Data.clear()
workforceService.requestWorkforce(const_code.toString(), jobtype.toString(), CodeList.sysCd, token.toString()).enqueue(object : Callback<SearchJoinWorkforceDTO> {
override fun onFailure(call: Call<SearchJoinWorkforceDTO>, t: Throwable) {
Log.d("retrofit", t.toString())
}
override fun onResponse( call: Call<SearchJoinWorkforceDTO>,response: Response<SearchJoinWorkforceDTO> ) {
workforce = response.body()
val len = workforce?.value?.size
Log.d("Retrofit", "데이터의 크기는 $len")
for (i in 0 until len!!) {
Log.d("Retrofit", "${i+1} 번째 데이터 넣는 중")
authority = workforce?.value?.get(i)?.authority_name.toString()
userName = workforce?.value?.get(i)?.user_name.toString()
coName = workforce?.value?.get(i)?.co_name.toString()
userPosition = workforce?.value?.get(i)?.user_position.toString()
workforce_Data.add(DetailDash_WorkForce(authority, userName, coName, userPosition))
}
binding.detaildashManpowerRecycler.adapter?.notifyDataSetChanged()
}
})
}
for(jobtype in 0..5)
{
Log.d("Retro", "${jobtype+1}번 째 루프 도는중..")
workforce(jobtype)
}
fun workforce(jobtype:Int) {
Thread{
workforce_Data.clear()
workforceService.requestWorkforce(const_code.toString(), jobtype.toString(), CodeList.sysCd, token.toString()).enqueue(object : Callback<SearchJoinWorkforceDTO> {
override fun onFailure(call: Call<SearchJoinWorkforceDTO>, t: Throwable) {
Log.d("retrofit", t.toString())
}
override fun onResponse( call: Call<SearchJoinWorkforceDTO>,response: Response<SearchJoinWorkforceDTO> ) {
workforce = response.body()
val len = workforce?.value?.size
Log.d("Retrofit", "데이터의 크기는 $len")
for (i in 0 until len!!) {
Log.d("Retrofit", "${i+1} 번째 데이터 넣는 중")
authority = workforce?.value?.get(i)?.authority_name.toString()
userName = workforce?.value?.get(i)?.user_name.toString()
coName = workforce?.value?.get(i)?.co_name.toString()
userPosition = workforce?.value?.get(i)?.user_position.toString()
workforce_Data.add(DetailDash_WorkForce(authority, userName, coName, userPosition))
}
binding.detaildashManpowerRecycler.adapter?.notifyDataSetChanged()
}
})
}.start()
try {
Thread.sleep(50)
} catch (e: Exception) {
e.printStackTrace()
}
}
반응형
'[Android APP] feat. Kotlin > Kotlin 공부' 카테고리의 다른 글
Kotlin : ImageView 이미지 확대, 축소, 회전 기능 (0) | 2023.02.02 |
---|---|
Kotlin : Thread, Handler (0) | 2023.02.02 |
Android Kotlin : 안드로이드 Activity 생명 주기 (0) | 2022.10.22 |
Android Kotlin : GlobalScope와 CoroutineScope (0) | 2022.10.22 |
Android Kotlin : MultiPart를 이용해 서버로 파일 보내기 (2) | 2022.09.21 |