[Error] Method setCurrentState must be called on the main thread

Minji JeongΒ·2022λ…„ 5μ›” 11일
0

Troubleshooting

λͺ©λ‘ 보기
1/20
post-thumbnail

문제

private val coroutineScope by lazy { CoroutineScope(Dispatcher.IO) }
private val infoSaveModule : DataStoreModule by inject() 

coroutineScope.launch {
	infoSaveModule.setData(1)
    Navigation.findNavController(binding.root).navigate(R.id.action)
}                         

코루틴 μŠ€μ½”ν”„ λ‚΄μ—μ„œ Jetpack Navigation을 μ‚¬μš©ν–ˆλ”λ‹ˆ λ‹€μŒκ³Ό 같은 였λ₯˜κ°€ λ°œμƒν–ˆλ‹€.

ν•΄κ²°

λ°±κ·ΈλΌμš΄λ“œ μŠ€λ ˆλ“œκ°€ μ•„λ‹Œ λ©”μΈμŠ€λ ˆλ“œμ—μ„œ Navigation을 μ‚¬μš©ν•˜κ±°λ‚˜, 코루틴 μŠ€μ½”ν”„μ˜ μŠ€λ ˆλ“œν’€μ„ λ©”μΈμŠ€λ ˆλ“œλ‘œ 지정해주면 ν•΄κ²°ν•  수 μžˆλ‹€. 각자의 λͺ©μ μ— 맞게 μ μ ˆν•˜κ²Œ μˆ˜μ •ν•˜μž.

1. λ©”μΈμŠ€λ ˆλ“œμ—μ„œ Navigation μ‚¬μš©

class MainFragment : Fragment() {
	private val coroutineScope by lazy { CoroutineScope(Dispatcher.IO) }
	private val infoSaveModule : DataStoreModule by inject() 

	private fun login(){
		...
		coroutineScope.launch {
			infoSaveModule.setData(1)
		}   
    	...
   	    Navigation.findNavController(binding.root).navigate(R.id.action)
	}
}
2. 코루틴 μŠ€μ½”ν”„μ˜ μŠ€λ ˆλ“œν’€μ„ λ©”μΈμŠ€λ ˆλ“œλ‘œ λ³€κ²½ν•΄μ„œ μ‚¬μš©

class MainFragment : Fragment() {
	private val coroutineScope by lazy { CoroutineScope(Dispatcher.Main) }
	private val infoSaveModule : DataStoreModule by inject() 
    ...
}

References

https://stackoverflow.com/questions/71329556/method-setcurrentstate-must-be-called-on-the-main-thread-android-kotlin

profile
Mobile Software Engineer

0개의 λŒ“κΈ€