Android 의 Cursor에서 Kotlin의 Closeable.use 를 쓸 경우엔 주의하자

in #android6 years ago

Kotlin의 Closeable은 use라는 유용한 확장함수를 제공한다.

안드로이드 앱을 개발할 때, DB나 ContentProvider에서 값을 가져오기 위해선 Cursor를 사용해야 하는데, 이 때 use를 이용하면 깔끔하게 작업을 처리할 수 있다.

하지만 릴리즈하고 보니 ICS 기기에서 다음과 같은 크래시가 접수되었다.

Caused by java.lang.ClassCastException
android.database.sqlite.SQLiteCursor cannot be cast to java.io.Closeable

왜죠?

검색해보니 stackoverflow에 다음과 같은 내용이 있었다.

Cursor는 젤리빈부터 Closeable 을 implement했어. ICS 시절의 Cursor는 Closeable이 아니야!

옆자리 동료가 플랫폼 코드를 diff 해보니 정말로 젤리빈 부터 Cursor가 Closeable을 구현하기 시작했다.

따라서! minSDK 16 미만인 경우 Cursor에 use 펑션을 쓰면 위험하다!

대충 다음과 같은 해결책을 생각할 수 있다.

  1. 이 기회를 틈타 minSDK 를 16 이상으로 올린다 (야호!)
  2. use를 안쓰던가, 안전하게 Cursor를 다룰 수 있는 짝퉁 use를 만들어서 사용한다.

난 2번을 택했다.

Sort:  

Congratulations @kingori2, you have decided to take the next big step with your first post! The Steem Network Team wishes you a great time among this awesome community.


Thumbs up for Steem Network´s strategy

The proven road to boost your personal success in this amazing Steem Network

Do you already know that awesome content will get great profits by following these simple steps that have been worked out by experts?

Congratulations @kingori2! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You got a First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 56934.21
ETH 3091.02
USDT 1.00
SBD 2.38