半空洞男女関係

思ったこととかプログラミングしてるときのメモとか色々かいてます。メールはidそのままgmail

UICollectionViewやUITableViewでperformBatchUpdatesを行う場合は、更新・削除・挿入を分ける

ドキュメントをよく読んでという話だけど、performBatchUpdates を行う際は、更新とアイテムの挿入・削除の順番に気を付ける必要がある。

performBatchUpdates(_:completion:) | Apple Developer Documentation

更新は先にやる

If the collection view's layout is not up to date before you call this method, a reload may occur. To avoid problems, you should update your data model inside the updates block or ensure the layout is updated before you call performBatchUpdates(_:completion:).

performBatchUpdates を呼ぶ前にデータの更新が終わってない場合に、リロードが発生する。 performBatchUpdates のupdateブロックの中か、 performBatchUpdates を呼び出す前に更新しておく必要がある。

A Tour of UICollectionView - WWDC 2018 - Videos - Apple Developer の中では、先に更新だけを行うperformBatchUpdatesを呼んでいたので、これに準拠するのが良さそう。

(performBatchUpdateで一気にやっていけない理由がまだ整理できていないので、あとでまとめたい。)

削除を先にやる

Deletes are processed before inserts in batch operations. This means the indexes for the deletions are processed relative to the indexes of the collection view’s state before the batch operation, and the indexes for the insertions are processed relative to the indexes of the state after all the deletions in the batch operation.

performBatchUpdates では、削除が先に実施され、挿入が後に実施されるようになっているので、削除と挿入をごちゃ混ぜにして作業してしまうとエラーが発生することがある。削除したいものと挿入したいものは区別して、一気に実施しなくてはいけない。

合わせて読みたい

A Tour of UICollectionView - WWDC 2018 - Videos - Apple Developer - mactkg-pub

WWDCのビデオを見ていて学習したことのメモ