Q:

CollectionView realoadData index out of range

I'm trying to fetch some data from coreData and display it in collectionView. Pretty basic. I do this on a background thread and then call reloadData on the main, as seen in the images. 
But the problem is that sometimes it works and sometimes it doesn't, giving me index out of range. And I observed that when it crashes it enters some delegate method of collection view and not viewWillAppear

Edit: Here Is the code

A:

Guys thank you for your trouble but i managed to obtain what i wanted.
I didn't solve the background issue instead i eliminated it. All i wanted to obtain was an activity indicator playing while the data was loaded from CoreData.
And by eliminating the queues it worked, which is pretty logical now that i think because i start the animation on the main thread and stop it after the whole data was loaded. So it seems the main thread "can do 2 things at the same time" ? :)

