0

I try to add a new cell in my UIcollectionView but it doesn't work. In fact I send a post request to my API, I wait for the response, when I get the response, I put de JSON content into an NSDictionnary.

My CellForItemAtIndexPath Datasource method loads cell content from an array called "formationsData", so when I get the response, I add my NSDictionnary into this array.

In numberOfItemAtIndexPath I return something like : [self.formationData count].

Here is my code when I post my request :

 NSDictionary * response = [responseJSON valueForKey:@"formation"];
    [(NSMutableArray *)_formationsView.carousel.formationsData addObject:response]; 
    [_formationsView.carousel insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem: _lastRowIndex-1 inSection:0]]]; //insert at index : last row - 1
//[_formationsView.carousel reloadData]

Before I was using reloadData but my app crash when I do that.

My problem : enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AnthonyR
  • 3,485
  • 1
  • 18
  • 41
  • 1
    Why did your app crash when you were using reloadData? – Teo Jun 05 '15 at 13:14
  • reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread. Maybe because I send an asynchonous request ? but my function fires only when I received data so I don't know. – AnthonyR Jun 05 '15 at 13:19

1 Answers1

1

Before I was using reloadData but my app crash when I do that.

Then that's the problem you need to solve. Just adding an item to the array you use to populate the collection view isn't enough to make a cell appear. The collection view doesn't have any idea where the data source gets its data; when there's a change to the data, you need to call -reloadData or similar.

If -reloadData causes a crash, then you need to figure out why and fix it.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • But I don't know why it causes a crash : erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.' I use didReceiveData, a delegate method to know when the response is back, and I use reload data in this function. I think i can't use reload data in a delegate function, I don't know... As I use an asynchronous request I can't use reloadData just after sending my post request, i'm forced to use my delegate method. – AnthonyR Jun 05 '15 at 13:33
  • 1
    You can absolutely call `-reloadData` in a delegate method, but calling it from a background thread is probably not a good idea. See [Objective C - Calling a method on the main thread?](http://stackoverflow.com/q/5606145/643383) for an example of how to call a method from the main thread. – Caleb Jun 05 '15 at 13:55
  • Thank you, you solved my problem. However if you can explain a little more because I didn't understand very well, when I call didReceivedData method, there is no background thread isn't it ? – AnthonyR Jun 05 '15 at 14:11
  • If you're loading data asynchronously, then yes, there's very likely a background thread involved. – Caleb Jun 05 '15 at 18:42
  • Yes, but when I tried to use reloadData in didReceiveData, The asynchonous request was normally done so I am a little confused. – AnthonyR Jun 07 '15 at 19:01