Crashlytics์์ ์ด์ ํด๋น ๋ฐ๊ฒฌ
๋ง์ด๋ ์ด์๋ก ๋ฑ์ฅํ๊ณ ์๋ค. (๊ณ ํ๋จํ๊ณ ์๋ค. ์ ๋ฐ ๋ง์ด๋ ์ด์์ฌ๋ผ...)
์ด์์ ๋ํ ์ค๋ช ๋ค
https://stackoverflow.com/a/3875204
Can't create handler inside thread that has not called Looper.prepare()
What does the following exception mean; how can I fix it? This is the code: Toast toast = Toast.makeText(mContext, "Something", Toast.LENGTH_SHORT); This is the exception: java.lang.RuntimeExce...
stackoverflow.com
https://gdlseed.tistory.com/70
์๋๋ก์ด๋ ์ฝํ๋ฆฐ / can't toast on a thread that has not called looper.prepare()
Toast ๋ฅผ ์ฌ์ฉํ๋ค๊ฐ can't toast on a thread that has not called looper.prepare() ์๋ฌ๊ฐ ๋์ค๋ฉด์ App ๋น์ ์ ์ข ๋ฃ๋ค ๋์๋ค. ๊ตฌ๊ธ๋ง ํ๋๋ UI ์ค๋ ๋๊ฐ ์๋ ์ค๋ ๋์์ ํ ์คํธ ์ฐฝ์ ๋์ฐ๋ ค๊ณ ํด์ ๋ฐ์ํ ๋ฌธ
gdlseed.tistory.com
์ฆ, Toast ํธ์ถ์ UI ์์ ์ด๋ผ Main Thread์์ ์์ ํด์ผ ํ๋๋ฐ, ์ค์ ๋ก Main Thread๊ฐ ์๋ ์ฐ๋ ๋์์ Toast ํธ์ถ์ด ์ผ์ด๋ ๊ฒ.
๊ทธ๋ผ ์ฝ๋์ ์ด๋์, ์ ๋ฐ์ํ๋๊ฐ
RetrofitService.INSTANCE.getMyApi()
.observeOn(Scheduler.io())
.subscribeOn(Scheduler.io())
.subscribe(data -> handleData(data),
error -> if (error != null) {
ExceptionExtensionsKt.handleErrorIfMyCustomException(error);
}
);
์๋ฐ๋ก ์์ฑ๋ ์ฝ๋์ด๋ค.
์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ๋ฉ์๋(์ ๋ถ๋ถ์์ ํ ์คํ ํธ์ถํ๋ค.) ํธ์ถ์ด Main Thread์์ ์ด๋ค์ง๋ ๊ฒ์ด ์๋ ๊ฒ์ ์ด์ ํ์ธํ๋ค.
์ ์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ๋ถ๋ถ์ Main Thread์์ ํธ์ถ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ ๋ง๊ธฐ๋ ํ๋ค.
์ด์๋ฅผ ์ด๋ป๊ฒ ์์ ํ ๊ฒ์ธ๊ฐ
๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ๋ค์ด ์๋ค.
1. ์ ์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๋ชจ๋ ๊ณณ์ ๋ค์ ธ์ Main Thread์์ ์คํํ๋๋ก ์์ ํ๋ค.
์ด๋ ๊ฒ ์งํํ ๊ฒฝ์ฐ ์์๋๋ ๊ฒ๋ค
- ์๊ณ ๋กญ๋ค.
- ์์ ํด์ผํ ๋ถ๋ถ์ ๋ด๊ฐ ๋์น ์ ์๋ค. ๊ฒฐ๊ตญ์ ๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
- ์ ์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ๋ฉ์๋์ ํ ์คํธ ํธ์ถ์ด ์๋ค๋ ๊ฑธ ์ธ์ํ์ง ๋ชปํ (ํ์คํ ๋ฆฌ๋ฅผ ์์ด๋ฒ๋ฆฐ ๋ฏธ๋์ ๋๋ฅผ ํฌํจํด์) ๋ค๋ฅธ ๊ฐ๋ฐ์๊ฐ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฌ์ฉ์ Main Thread๊ฐ ์๋ ๊ณณ์์ ํธ์ถํ๋๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค. ๊ทธ๋ผ ๋ ์ด์๊ฐ ๋ฐ์ํ๋ค.
2. ์ ์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ๋ฉ์๋ ์์์ ํ ์คํธ๋ฅผ ํธ์ถํ๋ ๋ถ๋ถ์ Main Thread์์ ์คํํ๋๋ก ์์ ํ๋ค.
์ด๋ ๊ฒ ์งํํ ๊ฒฝ์ฐ ์์๋๋ ๊ฒ๋ค
- 1๋ฒ๋ณด๋ค๋ ํจ์ฌ ๋ ์๊ณ ๋กญ๋ค.
- ์ ๋ฉ์๋๋ฅผ ์ฌ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฐ๋ฐ์๋ ์์ ํ๋ Thread์ ๋ํด์ ์ ๊ฒฝ์ธ ํ์๊ฐ ์๋ค.
2๋ฒ์ผ๋ก ์ ํํ๋ค.
์ด์ ์์
์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ์ฝ๋ ์์ ๊น๋ณด๊ณ ๋์ ๊ณผ๊ฑฐ์ ๋ด๊ฐ ๋์น ๋ถ๋ถ์ ๋ฐ๊ฒฌํ๋ค.๐คฆโ๏ธ
์๋ฌ ๊ณตํต ์ฒ๋ฆฌ ์ฝ๋ ์์ ์์ ๋ค์ด Main Thread์์ ์์ ํด์ผํ๋ ๊ฒ(ํ ์คํธ ํธ์ถ ๋ฑ)๊ณผ Thread์์ ์์ ํด์ผํ๋ ๊ฒ(๋ก์ปฌ ๋ฐ์ดํฐ ์ญ์ ๋ฑ)์ผ๋ก ๋๋์ด์ ธ ์๋๋ฐ, ์ฝ๋ ์์ฑ ๋น์ ์ด๊ฒ๋ค์ ๊ณ ๋ คํ์ง ์์๋ค. ๐คฆโ๏ธ
์ด๋ฒ ์ด์๋ก ๋ด๊ฐ ๋์น ๋ถ๋ถ์ ๋ฐ๊ฒฌํด์ ๋คํ์ด๋ค.
์ฝ๋ ์์ ์ ๋๋ต ์ด๋ฌํ๋ค.
fun Throwable.handleErrorIfMyCustomException() {
...
Thread {
//UI
Handler(Looper.getMainLooper()).post {
Timber.d("Toast Thread - ${Thread.currentThread().name}")
CustomToast.makeTest(context, R.string.msg, Toast.LENGTH_SHORT).show()
}
//Thread
Timber.d("Logout Thread - ${Thread.currentThread().name}")
LocalDataManager.deleteLocalData()
}.start()
...
}
์ฝํ๋ฆฐ์ผ๋ก ์์ฑํ ์ฝ๋์ด๋ค.
๋ก๊ทธ๋ก ๋์ ํ์ธ ์๋ฃ.
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Collapse 'catch' blocks (0) | 2023.02.08 |
---|---|
[Android][WebView] ์ฑ๊ณผ WebVeiw ๊ฐ ํต์ (0) | 2023.02.06 |
[Android][View] ๋ทฐ๊ฐ layout์ ์งํํ๋ ์ค์ requestLayout()์ ํธ์ถํ๋ฉด ์๋๋ค. (0) | 2023.01.12 |
[Android] ํ๋ฉด ์ ํ ์ ๋๋ฉ์ด์ (0) | 2023.01.12 |
[Android][Dialog] cancel / dismiss (0) | 2023.01.04 |