Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chaining performBatchUpdates for collectionview

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)

like image 593
SimplyLearning Avatar asked Oct 17 '22 14:10

SimplyLearning


1 Answers

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
        })
    })
like image 177
SuperDuperTango Avatar answered Oct 21 '22 02:10

SuperDuperTango