0

I'm using the standard method of fetching and parsing JSON through AFNetworking, the data is then used to populate a table view.

NSURL *url = [NSURL URLWithString:serverURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                     JSONRequestOperationWithRequest:request
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                                         [refresh endRefreshing];
                                         self.results = [json valueForKeyPath:@"data"];
                                         [self.tableView reloadData];
                                     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                         [refresh endRefreshing];
                                     }];
[self.tableView reloadData];
[operation start];

The way it is, every time the user pulls to refresh the entire table view disappears and new data is loaded and then presented to the user, after a second or so. However I want the data in the table to remain there, until and if new data is available and loaded. So I commented out the [self.tableView reloadData]; thinking that would make it work, whilst visibly and for the first few seconds it does, however the application crashes soon after. With the error:

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds for empty array'

Now I am not even entirely sure this is the right way to go about doing it, so basically, my question is how would I keep the data in the table view until new data is loaded, or push the existing data down the table view, sort of like the Facebook and Twitter apps do when you refresh the news feed/timeline.

UPDATE

I tried this method:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.results count] inSection:0];
NSArray *indexArray = [NSArray arrayWithObjects:indexPath, nil];
[self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];

But I get the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 15 into section 0, but there are only 15 rows in section 0 after the update'

Alex Saidani
  • 1,277
  • 2
  • 16
  • 31

1 Answers1

0

In your success block, you will have to first check if you are getting the data. If there is any data, then reload your tableview with the new data, else you should not attempt to change the existing data. Try this.

  NSArray *tempResults = [json valueForKeyPath:@"data"];
  if(tempResults && [tempResults count] > 0)
  {                                             
      self.results = tempResults;                                        
  }                                        
  [self.tableView reloadData];

Keep a breakpoint on tempResults and see what data are you getting.

nithinbhaktha
  • 723
  • 5
  • 8
  • Thanks for the answer, but that doesn't really answer the question. I'm trying to add the new data on top of the existing data, rather than simply reloading the table with the new data. – Alex Saidani Aug 06 '13 at 12:58