ListChange
This sealed interface describes the possible changes that can happen to a RealmList collection.
The states are represented by the specific subclasses InitialList, UpdatedList and DeletedList. When the list is deleted an empty list is emitted instead of null
.
Changes can thus be consumed in a number of ways:
// Variant 1: Switch on the sealed interface
person.addresses.asFlow()
.collect { listChange: ListChange<Address> ->
when(listChange) {
is InitialList -> setAddressesUIList(listChange.list)
is UpdatedList -> updateAddressesUIList(listChange) // Android RecyclerView knows how to animate ranges
is DeletedList -> deleteAddressesUIList()
}
}
// Variant 2: Just pass on the list
person.addresses.asFlow()
.collect { listChange: ListChange<Address> ->
handleChange(listChange.list)
}
Content copied to clipboard
When the list is updated, extra information is provided describing the changes from the previous version. This information is formatted in a way that can be feed directly to drive animations on UI components like RecyclerView
. In order to access this information, the ListChange must be cast to the appropriate subclass.
person.addresses.asFlow()
.collect { listChange: ListChange<Address> ->
when(listChange) {
is InitialList -> setList(listChange.list)
is UpdatedList -> { // Automatic cast to UpdatedList
updateList(
listChange.list,
listChange.deletionRanges,
listChange.insertionRanges,
listChange.changeRanges
)
}
is DeletedList -> deleteList()
}
}
Content copied to clipboard