Home > Blockchain >  Run the code in main threa after completeion of IO thread process kotlin coroutines
Run the code in main threa after completeion of IO thread process kotlin coroutines

Time:09-27

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

  lifecycleScope.launch(Dispatchers.Main) {
            networkviewModel.productist.collectLatest {
                when (it) {
                    Resource.Empty -> {
                        Log.e("product", ""   "empty")
                    }
                    is Resource.Failure -> {
                        Log.e("product", ""   "failure")
                    }
                    Resource.Loading -> {

                    }
                    is Resource.Success -> {
                        val response = it.value
                        Log.e("test","success response")
                        val products: List<Products> = response.data
                        val variants: List<Variants> = response.data.map { it.variants }.flatten()
                        lifecycleScope.launch(Dispatchers.IO) {
                            productRoom.insertProducts(products)
                            productRoom.insertVariants(variants)
                            val subcategoryid = SubCategoryList(response.data[0].subcategory_id!!.toInt())
                            productRoom.insertSubCat(subcategoryid)
                        }
                        subcat_id = response.data[0].subcategory_id!!.toInt()
                        roomviewModel.productFlow.collectLatest{
                            Log.e("test","flow success from network room")
                            //    Log.e("test",it.toString())
                            LoadingUtils.hideDialog()
                            proadapter.setMutableArraylist(it)
                            proadapter.notifyDataSetChanged()
                        }
                    }
                }
            }
        }
}

i got error of FATAL EXCEPTION: main Process: com.store.pasumainew, PID: 13125 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

because in Resource.Success -> collect called before the completion of IO thread process

I want to complete the IO thread and afterwards collect the value from room

CodePudding user response:

Try to use withContext(Dispatchers.IO) before collecting, it will be suspended until all operations are complete:

withContext(Dispatchers.IO) {                
    productRoom.insertProducts(products)
    productRoom.insertVariants(variants)
    val subcategoryid = SubCategoryList(response.data[0].subcategory_id!!.toInt())
    productRoom.insertSubCat(subcategoryid)
}
// ... collecting code

Btw you don't need to use Dispatchers.Main to launch the main coroutine, by default lifecycleScope works on Dispatchers.Main context, so you can just write:

lifecycleScope.launch {
    // ...
}

CodePudding user response:

The crash is in this line of code

val subcategoryid = SubCategoryList(response.data[0].subcategory_id!!.toInt())

try changing it to this

val subcategoryid = SubCategoryList(response.data[0]?.subcategory_id?.toInt())?:0
  • Related