oe a
oe a

Reputation: 2280

Crash when pull to refresh table

I'm having a very strange bug with my pull to refresh. I have a function that refreshes my data for my table. It is called both by pull to refresh and on viewWillAppear. This same function works perfectly fine for the latter but crashes when pulling to refresh. Here is my 'reset' function

-(void)reset{
    refreshList = nil;
    isFiltered = NO;
    currentRoom = nil;
    [roomList removeAllObjects];
    NSMutableURLRequest *newRequest=[NSMutableURLRequest requestWithURL:
        [NSURL URLWithString:@"http://192.168.1.128:8080/DJService/room/list/"]
                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
    [newRequest setHTTPMethod:@"POST"];
    refreshList = [[NSURLConnection alloc] initWithRequest:newRequest
                                                  delegate:self];
}

Now, when running pull to refresh here's what its doing. It goes straight to the cellForRowAtIndexPath function and crashes on this line:

room = [roomList objectAtIndex:indexPath.row];

It's crashing because roomList is empty and hasn't been populated yet. What I don't understand is why it's going to this function when reloadData has not been called yet. From the above code, a url connection is made to fetch new data and then the table is supposed to be reloaded in connectionDidFinishLoading however that function is never called, in fact none of the other delegate functions for my table or URL connection are called. It goes straight to cellForRowAtIndexPath and tries to load an empty list....why is it doing this?

This exact same function (reset) is called in viewWillAppear and works fine.
Making things even more confusing is that this crash only happens when I have 7 or more elements in my table (filling my screen up)

Upvotes: 1

Views: 668

Answers (1)

Nick C
Nick C

Reputation: 514

Hold a cache of the roomList so that you know that you're updating it and that the data isn't up to date. The reason it calls cellForRowAtIndexPath when you fill up the screen is because of the dequeueing of cells offscreen. I wouldn't leave the table with no data to pull. Also, if you didn't write the pull to refresh controller, it might be calling reloadTable.

Upvotes: 6

Related Questions