batch Notify Async
Executes a suspending block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.
This is the asynchronous equivalent of batchNotify, suitable for operations that involve coroutines or other suspending functions. This is particularly useful when you need to perform async operations (like network calls or database queries) as part of your collection updates.
Example:
val userCache = reactiveMapOf<String, User>()
// Batch async operations - single emission at the end
userCache.batchNotifyAsync {
// Simulate async operations
val user1 = userRepository.fetchUser("alice") // suspending call
val user2 = userRepository.fetchUser("bob") // suspending call
put("alice", user1)
put("bob", user2)
remove("charlie") // Remove cached user
delay(100) // Some async processing
} // Observers are notified only once here with all updatesContent copied to clipboard
Parameters
block
A suspending lambda function with the mutable collection as its receiver.