title: Pull to refresh crashes app
body: <p>I have an app, that takes events from 3 shared Google Calendars and displays it in a table view. </p>
  
  <p>I wanted to implement pull to refresh, but the app keeps crashing, if I let go from the pull, before the data is loaded. (if I hold the pull for a couple of seconds everything is fine - if I let go immediately it crashes.</p>
  
  <p>Code:</p>
  
  <pre><code>-(void)viewDidLoad
  {
      [super viewDidLoad];    
      UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
      refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
      [refresh addTarget:self action:@selector(getEvents) forControlEvents:UIControlEventValueChanged];
      self.refreshControl = refresh;
      startDates = [[NSMutableArray alloc] init];
      [self getEvents];
  }
  
  - (void)stopRefresh
  {
      [self.refreshControl endRefreshing];
  }
  
  -(void)getEvents
  {
      [startDates removeAllObjects];
      startDates = [NSMutableArray array];
      sectionEntries = [NSMutableArray array];
      entries = [NSMutableArray array];
      sortedStartDates = [[NSArray alloc]init];
      _imageForCalendarType = [[NSDictionary alloc]init];
      _imageForCalendarType = @{
                                @"The Irish House Music Calendar" : [UIImage imageNamed:@"music.png"]
                                ,   @"FixedEvents-Student Night"  : [UIImage imageNamed:@"student.png"]
                                ,   @"FixedEvents-Ladies Night"         : [UIImage imageNamed:@"cocktail.png"]
                                ,   @"AppTest"         : [UIImage imageNamed:@"football.png"]
                                };
      dispatch_async(kBgQueue, ^{
  
          NSData* data = [NSData dataWithContentsOfURL:sportsCalendarURL];
          [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
  
          NSData* data2 = [NSData dataWithContentsOfURL:musicCalendarURL];
          [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data2 waitUntilDone:YES];
  
          NSData* data3 = [NSData dataWithContentsOfURL:fixedCalendarURL];
          [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data3 waitUntilDone:YES];
  
          // Reload table view - UI operation, so must be run on main thread
          dispatch_async(dispatch_get_main_queue(), ^{
  
              sortedStartDates = [startDates sortedArrayUsingSelector:@selector(compare:)];
              [self.tableView reloadData];
              [self performSelector:@selector(stopRefresh) withObject:nil afterDelay:2.5];
          });
      });
  
  
  }
  </code></pre>
  
  <p>It gives me a SIGABRT error in this line in the cellForRowAtIndexPath method:</p>
  
  <pre><code> NSInteger index = [self getRow:sortedStartDates[indexPath.section]];  // get correct index for sectionEntries
  </code></pre>
  
  <p>Error:  <strong>* Terminating app due to uncaught exception 'NSRangeException', reason: '*</strong> -[__NSArrayI objectAtIndex:]: index 4 beyond bounds for empty array'</p>
  
  <p>It seems like the error is because theres no data in my startDates NSMutableArray, but if i comment the line [startDates removeAllObjects] I get redundant cells.</p>