batch Notify
Executes a block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.
This is useful for performing multiple additions, removals, or other mutations in a single, atomic operation, preventing multiple emissions from the StateFlow. This is particularly important for performance when making bulk changes, as it avoids triggering multiple reactive updates that could cause unnecessary recomputations in observers.
Example:
val shoppingList = reactiveListOf<String>()
// Without batch - triggers 3 separate emissions
shoppingList.add("Apple")
shoppingList.add("Banana")
shoppingList.add("Orange")
// With batch - triggers only 1 emission at the end
shoppingList.batchNotify {
add("Milk")
add("Bread")
removeAt(0) // Remove "Apple"
set(1, "Blueberries") // Replace "Banana"
} // Observers are notified only once here with final stateContent copied to clipboard
Parameters
block
A lambda function with the mutable collection as its receiver, where modifications can be performed.