Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit e364ee1

Browse files
committed
use DispatchQueue.main.async for collectionView.performBatchUpdates
1 parent 4135eb5 commit e364ee1

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

Lib/CollectionViewPagingLayout.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
222222

223223
// this is necessary when we want to set the current page without animation
224224
if !animated, page != currentPage, let collectionView = collectionView {
225-
collectionView.performBatchUpdates({
226-
collectionView.collectionViewLayout.invalidateLayout()
227-
})
225+
DispatchQueue.main.async {
226+
collectionView.performBatchUpdates({
227+
collectionView.collectionViewLayout.invalidateLayout()
228+
})
229+
}
228230
}
229231
}
230232

Samples/PagingLayoutSamples/Modules/LayoutDesigner/Code/LayoutDesignerCodePreviewViewModel.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ struct LayoutDesignerCodePreviewViewModel {
137137
138138
override func viewDidLayoutSubviews() {
139139
super.viewDidLayoutSubviews()
140-
collectionView?.performBatchUpdates({
141-
self.collectionView.collectionViewLayout.invalidateLayout()
142-
})
140+
DispatchQueue.main.async { [weak self] in
141+
self?.collectionView?.performBatchUpdates({ [weak self] in
142+
self?.collectionView?.collectionViewLayout.invalidateLayout()
143+
})
144+
}
143145
}
144146
145147
func collectionView(

Samples/PagingLayoutSamples/Modules/LayoutDesigner/Intro/LayoutDesignerIntroViewController.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class LayoutDesignerIntroViewController: UIViewController {
3636

3737
override func viewDidLayoutSubviews() {
3838
super.viewDidLayoutSubviews()
39-
collectionView?.performBatchUpdates({ [weak self] in
40-
self?.collectionView.collectionViewLayout.invalidateLayout()
41-
})
39+
DispatchQueue.main.async { [weak self] in
40+
self?.collectionView?.performBatchUpdates({ [weak self] in
41+
self?.collectionView?.collectionViewLayout.invalidateLayout()
42+
})
43+
}
4244
}
4345

4446

Samples/PagingLayoutSamples/Modules/Shapes/ShapesViewController.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,20 @@ class ShapesViewController: UIViewController, NibBased, ViewModelBased {
155155
invalidateCollectionViewLayout()
156156
}
157157

158-
private func invalidateLayoutTypeCollectionViewLayout() { layoutTypeCollectionView?.collectionViewLayout.invalidateLayout()
159-
layoutTypeCollectionView?.performBatchUpdates({
160-
self.layoutTypeCollectionView.collectionViewLayout.invalidateLayout()
161-
})
158+
private func invalidateLayoutTypeCollectionViewLayout() {
159+
DispatchQueue.main.async { [weak self] in
160+
self?.layoutTypeCollectionView?.performBatchUpdates({ [weak self] in
161+
self?.layoutTypeCollectionView?.collectionViewLayout.invalidateLayout()
162+
})
163+
}
162164
}
163165

164166
private func invalidateCollectionViewLayout() {
165-
collectionView?.collectionViewLayout.invalidateLayout()
166-
collectionView?.performBatchUpdates({
167-
self.collectionView.collectionViewLayout.invalidateLayout()
168-
})
167+
DispatchQueue.main.async { [weak self] in
168+
self?.collectionView?.performBatchUpdates({ [weak self] in
169+
self?.collectionView?.collectionViewLayout.invalidateLayout()
170+
})
171+
}
169172
}
170173

171174
private func getPagingLayout(_ collectionView: UICollectionView) -> CollectionViewPagingLayout? {

0 commit comments

Comments
 (0)