The app I'm building has a lot of tables. I'm loading data for the first 15 rows, and when the user scrolls down, I'm getting the next batch of 15 rows.
I have written some DBAccess functions that takes in a 'count' and 'skip'. When loading the table initially, count = 15, skip = 0. When the user scrolls, count = 15, skip = [array count];
The code is something like this:
- (void)getDataWithCount:(NSInteger)count withSkip:(NSInteger)skip
{
if (skip == 0)
self.dataArray = [[NSMutableArray alloc]init];
[DBAccess getData:self.dataArray withCount:count withSkip:skip handler:^(void)
{
if (no error)
[table reloadData];
}];
}
I get array out of bounds crashes in cellForRowAtIndexPath on really slow network connections (e.g., index 16 beyond empty array. numberOfRowsInSection returns [array count]).
I think the issue is as follows:
0) enter the screen.
1) Load 15 rows
2) Scroll down, load another 15. Slow connection, so I don't get a call back yet from my sendAsynchronousRequest db call
3) Go to another viewcontroller (i.e., leave the screen. I can do this because async doesn't freeze up my screen).
4) Come back into the screen. Loading initial screen sets skip = 0, hence initialises my mutable array, hits the database
5) The database hit from step 2 returns. My mutable array has been set to zero now from step 4. When reloading the table, it's trying to get self.dataArray objectAtIndex:16 etc, when the array is now empty from if(skip == 0)initialise
Does this seem about right? Any other reason why I would have array out of bounds if numberOfRowsInSection is returning [array count];? Is there a preferred way to fix this?