asyncTask 가 deprecated 되는 이유와 앞으로는 어떤걸 사용하는지

Kyung yup Lee·2020년 12월 31일
0

안드로이드

목록 보기
8/8

asyncTask

asyncTask api는 오랜 기간 동안 안드로이드에서 UI 스레드에 직접 접근할 수 있도록 해주어 데이터의 변화를 보여줄 수 있는 소중한 역할을 했다.

deprecated 되는 이유

먼저 안드로이드 공개 코드 헤드부분에 주석으로 달려있던 코드를 보면

<p>
* AsyncTask enables proper and easy use of the UI thread. This class allows you
* to perform background operations and publish results on the UI thread without
* having to manipulate threads and/or handlers</p>

이 내용이

 * <p>AsyncTask was intended to enable proper and easy use of the UI thread. However, the most
 * common use case was for integrating into UI, and that would cause Context leaks, missed
 * callbacks, or crashes on configuration changes. It also has inconsistent behavior on different
 * versions of the platform, swallows exceptions from {@code doInBackground}, and does not provide
 * much utility over using {@link Executor}s directly.</p>

로 바뀌었다.

간단히 해석하면 asyncTask는 UI 스레드를 사용하기 위한 적절한 api였고, 스레드를 복제하거나 핸들러를 사용하지 않고도 백그라운드 작업을 하면서 UI를 업데이트를 할 수 있게 해주는 api였다. 하지만 이 api가 가장 많이 사용되는 방식인 UI로의 통합이 컨텍스트 leak나 콜백을 빼먹거나, 설정 변화의 충돌로 인해 많은 문제를 일으켰고, 버젼 문제까지 일으키면서 더 이상 유용성을 제공하지 못한다고 판단하여 deprecated 되게 되었다.

이를 대체하기 위해 rxJava, coroutine 등의 사용이 가능하다.

코틀린을 이용해 비동기 처리가 가능하기 때문에 coroutine을 사용한다.

코루틴이 그냥 간단한 라이브러리인줄 알았는데, 코루틴은 코틀린에만 존재하는 것이 아니라 다른 다양한 언어에서도 지원하고 있는 추상적인 "개념" 이었다.

물론 이 개념을 이용해 구체적인 라이브러리를 만들어내고 그걸 우리가 사용하기는 한다.

만만하게 생각했는데 쉬운 개념이 아닌 것 같다. 공부해서 따로 이 부분에 대한 내용을 정리해야 겠다.

profile
성장하는 개발자

0개의 댓글