I am finding that my followCollectionView is not reloading at this very specific place in my code.
func unFollowBtnPressed() {
if let itemPaths = followCollectionView.indexPathsForSelectedItems() {
unFollowPosts(itemPaths)
} else {
// No item to unfollow
}
self.followCollectionView.reloadData()
}
func unFollowPosts(itemPaths: [NSIndexPath]) {
followCollectionView.performBatchUpdates({
let sortedIndexPathArray = itemPaths.sort{$1.row < $0.row}
for indexP in sortedIndexPathArray {
DataService.ds.removeFollow(PostService.ps.followingPosts[indexP.row])
PostService.ps.followingPosts.removeAtIndex(indexP.row)
}
self.followCollectionView.deleteItemsAtIndexPaths(sortedIndexPathArray)
}) { (success: Bool) in
}
}
The program executes reloadData() but cellForItemAtIndexPath was never called (Everything is hooked up right because other places calling reloadData works fine). I am thinking it is a timing issue because if I re-arrange where I reload data, the collection view was able to be re-loaded. Below is the working code. I am not sure why the above method work and the below method doesn't. I have tried to wrap the reloadData() code in dispatch_async(dispatch_get_main_queue()) and it still didnt work?
func unFollowBtnPressed() {
if let itemPaths = followCollectionView.indexPathsForSelectedItems() {
unFollowPosts(itemPaths)
} else {
// No item to unfollow
self.followCollectionView.reloadData()
}
}
func unFollowPosts(itemPaths: [NSIndexPath]) {
followCollectionView.performBatchUpdates({
let sortedIndexPathArray = itemPaths.sort{$1.row < $0.row}
for indexP in sortedIndexPathArray {
DataService.ds.removeFollow(PostService.ps.followingPosts[indexP.row])
PostService.ps.followingPosts.removeAtIndex(indexP.row)
}
self.followCollectionView.deleteItemsAtIndexPaths(sortedIndexPathArray)
}) { (success: Bool) in
self.followCollectionView.reloadData()
}
}
performBatchUpdates:completion:seems to be asynchrone. For instance, try do do aprintfinside it, and in yourelse. Check what's the first one to show. - LarmereloadDatasets a flag and starts the reload process asynchronously. If the flag is already set, no new update is triggered. Thus, callingreloadDatain the middle of an update is essentially a no-op. - Avi