0
votes

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()
    }
}
1
performBatchUpdates:completion: seems to be asynchrone. For instance, try do do a printf inside it, and in your else. Check what's the first one to show. - Larme
I was guessing that but that shouldnt stop the reloadData from reloading the collectionView though (cellForRowAtIndexPath does not get called in the code on the top) - user172902
Calling reloadData sets a flag and starts the reload process asynchronously. If the flag is already set, no new update is triggered. Thus, calling reloadData in the middle of an update is essentially a no-op. - Avi
@Avi, that would make sense. However, when I was using the "not working code" I had a breakpoint set at cellForItemAtIndexPath function. However, despite the reloadData() function gets called, the cellForItemAtIndexPathNever. If there was a flag set, wouldn't that mean reloadData would be in a queue somewhere waiting to be executed? - user172902

1 Answers

0
votes

did you try these:

DispatchQueue.main.async(execute: {
    //reload
})

OperationQueue.main.addOperation({
    //reload
})