KQLite Flow
KQLite allows KQLiteCursor to be converted into a cold Flow that reexecutes query and emits the cursor whenever the underlying database tables are updated. It observes INSERT, UPDATE and DELETED onto the specified table which is used to create cursor. If the cursor is not created using SELECT SelectStatement or has no associated tables in it, the resulting flow will emit the cursor once and then complete. It does not re-emit the cursor for queries other than SELECT statement. For more details see
fun KQLiteCursor.asCallbackFlow(): Flow<KQLiteCursor>
val flow =
TblContact
.select()
.where { it.deleted NOT_EQ true }
.orderBy(TblContact.firstName)
.execute()
.asCallbackFlow()
.mapToList(mapper = TblContact::mapper)
Content copied to clipboard
Example Usage
// Obtaining simple KQLiteCursor, it is also an Iterator.
val cursor: KQLiteCursor =
TblContact.quickSelect()
// Converting KQLiteCursor to Sequence for more features
val sequence: Sequence<KQLiteCursor> =
TblContact.quickSelect().asSequence()
// Getting row count from KQLiteCursor
val count: Int =
TblContact.quickSelect().asSequence().count()
// Mapping KQLiteCursor to list of entities, TblContact implements KQLiteAdapter
val contactList: List<Contact> =
TblContact.quickSelect().mapToList(TblContact::mapper)
// Reading single row. Throws Exception if count() != 1
val singleContact: Contact =
TblContact.quickSelect().mapToSingle(TblContact::mapper)
// Reading single item or null
val nullableContact: Contact? =
TblContact.quickSelect().mapToSingleOrNull(TblContact::mapper)
// KQLiteCursor custom mapping
val nameList: List<String> =
TblContact.quickSelect().mapToList { it[TblContact.firstName] }
// Converting KQLiteCursor to Flow. It does not observe table changes.
val flow: Flow<KQLiteCursor> =
TblContact.quickSelect().asFlow()
// Mapping Flow as usual
val nameFlow: Flow<String> =
TblContact.quickSelect().asFlow().map { it[TblContact.firstName] }
// Converting KQLiteCursor to Flow. Observe table changes INSERT, UPDATE and DELETE.
val callbackFlow: Flow<KQLiteCursor> =
TblContact.quickSelect().asCallbackFlow()
// Mapping Flow to List of observable entities.
val contactsFlow: Flow<List<Contact>> =
TblContact.quickSelect().asCallbackFlow().mapToList(mapper = TblContact::mapper)
// Custom dispatcher, default is IO, if not given.
val dispatcher: Flow<List<Contact>> =
TblContact.quickSelect().asCallbackFlow().mapToList(Dispatchers.Main, TblContact::mapper)
// Observable Flow of nullable item.
val singleFlow: Flow<Contact?> =
TblContact.quickSelect().asCallbackFlow().mapToSingleOrNull(mapper = TblContact::mapper)
Content copied to clipboard