- To fix the flash? lightening bug when scrolling the search results I changed reloadData when paging to “insertItems”
- After that, when I need to reloadData from SearchResults to SearchHistory, cellForItem is not called for section 0
- The reason was realodData is called before insertItems finished
- So I made the orders guaranteed using dispatchGroup.
private let insertItemsAndReloadDataGroup = DispatchGroup()
... after getting paged results ....
self.insertItemsAndReloadDataGroup.enter()
self.collectionView.performBatchUpdates {
self.collectionView.insertItems(at: indexPaths)
} completion: { _ in
self.insertItemsAndReloadDataGroup.leave()
}
... when I go back to searchHistory from searchResult ...
insertItemsAndReloadDataGroup.notify(queue: .main) { [weak self] in
guard let self = self else { return }
self.reloadDataAndResetScroll()
5. After that I had other problems. Because I used different numbers of sections according to the types of search results. So I changed the code to reloadData when it is not paging, I mean it’s first page.