I think the problem maybe at [self downloadImage_3:indexPath];
you call dispatch_async
when you at Button1
, and the dispatch_async
block have not invoked, then you click on Button2
, then I guess msg_array
is cleared and filled with new 5 object, after that, dispatch_async
block is invoked, the path.row
for block is 28, whereas, the msg_array
has new array content with 5 new objects, then crash.
You should cancel dispatch_async
before click on other button, which is impossible for dispatch_async
, so you can have a judgement in block:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundFetchTask];
[self downloadImage_3:indexPath buttonIndex:index];
[self endBackgroundFetchTask];
});
-(void)downloadImage_3:(NSIndexPath *)path buttonIndex:(int)buttonIndex{
if(self.currentSelectIndex != buttonIndex) return; //skip reloadData if not same index
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ImagePath]]];
if (img) {
[dicImages_msg setObject:img forKey:[[msg_array objectAtIndex:path.row] valueForKey:@"Merchant_SmallImage"]];
}
[_tblList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}