I have a situation where the code change its collectionView data source then call reloadData()
several times and i think this cause my app to crash due to the race condition as the data source change very fast, so I wrapped the snippet of code that (change data source then reload collection view) with DispatchQueue.main.async
and the crash gone.
So the question Does this make sense and solved the race condition, what i understand is wrapping this snippet of code with DispatchQueue.main.async
will not execute this snippet again until the reloadData() function finishes, Is that right?
if you have any other thoughts about how to solve this issue it will be great
Update:
For example if we have this code on main thread
self.collectionView.dataSource = self.dataSource
self.collectionView.reloadData()
and this snippet of code called twice one after another fast so the data source changed while the first reload in progress, as a solution i wrapped this snippet of code with async block to make sure the data source will not change until the first reload finish. like the below:
DispatchQueue.main.async {
self.collectionView.dataSource = self.dataSource
self.collectionView.reloadData()
}
Is this will guarantee that the data source will not change until the reloadData()
finish ?