only the original thread that created a view hierarchy can touch its views.

Heejin Ryu·2021년 7월 16일
0

Room에 데이터를 저장하는 것에 삽질을 하던 도중에 간헐적으로 이 에러가 떴다.

찾아보니, Thread관련 에러로, 메인 스레드 외에 다른 스레드를 생성하여 UI를 바꾸려고 해서 나는 문제였다.

안드로이드의 경우 ui는 무조건 메인스레드에서 관리되기 때문에 이 부분을 추가하지 않았나 하고 보니, subscribeOnobserveOn 이 헷갈려서 아래와 같이 코드를 써놨다.

profileViewModel.isUserInfoCompletedSubject
            .observeOn(Schedulers.io())
            .subscribe({
                binding.completedButton.isEnabled = it
            }, {
                Log.e("User information input is all filled", it.localizedMessage)
            })
            .addToDisposables()

RxAndroid에게 지금 ui를 변경하는 mainThread를 사용하고있다는 것을 알려야하기 때문에, 아래와 같이

io 스케줄러에서 subscribe를 하고, mainThread에서 observe를 하고 있다고 코드를 작성했었어야 했다.

profileViewModel.isUserInfoCompletedSubject
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                binding.completedButton.isEnabled = it
            }, {
                Log.e("User information input is all filled", it.localizedMessage)
            })
            .addToDisposables()
profile
Chocolate lover🍫 & Junior Android developer🤖

0개의 댓글