android / android-ktx

A set of Kotlin extensions for Android app development.
https://android.github.io/android-ktx/core-ktx/
7.48k stars 565 forks source link

Add empty check functions #573

Open pablisco opened 6 years ago

pablisco commented 6 years ago

Extends Cursor to provide functions to check i the cursor is empty or not

Usage

if(cursor.isEmpty()) {
  // we can show an empty screen
}

if(cursor.isNotEmpty()) {
  // We got data
}
JakeWharton commented 6 years ago

I don't think we want to use count because it requires that the query backing the cursor traverse its entire result set. So if you query for a million items but end up only taking 5 calling count will traverse over the entire million to return an accurate number.

pablisco commented 6 years ago

Good point, would isBeforeFirst be a better call? I do hope not many people have 5 million records on their app database (but wouldn't surprise me either hehe)

pablisco commented 6 years ago

I was also contemplating creating a toSequence(Cursor.() -> T) : Sequence<T> which would may make it easier to use. But I'm not sure if it's out of the scope of ktx as it's kind of a new feature.