I've a UIViewController
that contains a UITableView
and when I call the method "requireObjects", this last populate the arrays to configure the table. I state that the cellForRowAtIndexPath
method work properly and dequeue a number of cells based on [_arrID count]
.
Each populated array has a matching index with all other arrays. For example arrID[5] = 453
corresponds to arrNames[5] = "Jean"
and corresponds to arrStyle[5] = "Angry"
and corresponds to arrLocation[5] = "New York"
and corresponds to arrProfileSnaps[5] = jean.png
.
These 5 arrays for example make up the fifth cell in the tableView. The method "requireObjects" succeeds and the table is built correctly. The problem arises when I call this method while it is still running. I wish that the method stops and repeats when I click a button. Or desperate times, it's good also that the method ends its execution and repeat when I click the button. But unfortunately my app crashes. Here the code:
- (void)requireObjects{
@try {
//threadsQueue is a NSOperationQueue allocated and initialized in viewDidLoad method
[threadsQueue cancelAllOperations];
[threadsQueue waitUntilAllOperationsAreFinished];
[_arrID removeAllObjects];
[_arrNames removeAllObjects];
[_arrStyle removeAllObjects];
[_arrLocation removeAllObjects];
[_arrProfileSnaps removeAllObjects];
_arrNames = [[NSMutableArray alloc] init];
_arrStyle = [[NSMutableArray alloc] init];
_arrLocation = [[NSMutableArray alloc] init];
_arrProfileSnaps = [[NSMutableArray alloc] init];
[threadsQueue addOperation:[NSBlockOperation blockOperationWithBlock: ^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CGRect activityFrame = CGRectMake(self.view.center.x, self.view.center.y, 0.0, 0.0);
_activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityFrame];
[[self.view superview] addSubview:_activityIndicator];
_activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
_activityIndicator.color = [UIColor whiteColor];
[_activityIndicator startAnimating];
[_tableView reloadData];
}];
_arrID = [[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]];
for (int i = 0; i < [_arrID count]; i++) {
[_arrStyle addObject:@“..”];
[_arrNames addObject:@".."];
[_arrLocation addObject:@“..”];
[_arrProfileSnaps addObject:[UIImage imageNamed:@"blank"]];
}
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
[self.tableView reloadData];
[_activityIndicator stopAnimating];
}];
}]];
}
@catch (NSException *exception) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"An exception has occurred" message:[NSString stringWithFormat:@"%@",exception] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
}
The app crashes in one iteration of the for loop at the point where I do [_arrNames insertObject:...
. The error:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
The strange thing is that the method succeeds any time I call it, but crashes only when I call it while it's already running!