What is the proper way to chain performBatchUpdate
calls for UICollectionView
such that one update completes before the next begins, to prevent multiple firing at the same time and hence leading to IndexPaths not syncing up (due to deletions mixed in with insertions)
I'm not quite sure what you're looking for here. If you mean how do you ensure that multiple threads don't overwrite each other when doing performBatchUpdate
, you should ensure that all of your calls to performBatchUpdate
are called on the main thread. e.g.:
DispatchQueue.main.async {
collectionView.performBatchUpdates({
// perform a bunch of updates.
}, completion: { (finished: Bool) in
// anything that needs to happen after the update is completed.
}
}
Alternatively, if you're looking for a way to call performBatchUpdates
multiple times in the same thread (yeah, it's somewhat odd that you'd need this, but I have a place where I need to because of a 3rd party API I'm using), since performBatchUpdates
has a completion, you might try putting the second performBatchUpdates
call in the completion handler of the first.
self.collectionView.performBatchUpdates({
// perform a bunch of updates.
}, completion: { (finished: Bool) in
self.collectionView.performBatchUpdates({
// perform a bunch of other updates.
}, completion: { (finished: Bool) in
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With