I’m using the following code to populate a UITableViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSString *selectedWord;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
ResultDetail *detail = [[[GameStore defaultStore] currentResultDetail] objectAtIndex:[indexPath row]];
if([[detail selectedWord] isEqualToString:@" "])
{
selectedWord = @" selected: n/a";
}
else{
selectedWord = [NSString stringWithFormat:@" selected: %@", [[detail selectedWord] substringFromIndex:4]];
}
[[cell textLabel] setText:[NSString stringWithFormat:@" %@ (score: %d/4)",[detail word],[detail score]]];
[[cell detailTextLabel] setText:selectedWord];
detail = nil;
[detail release];
selectedWord = nil;
[selectedWord release];
return cell;
}
The above code run fine given that value of currentResultDetail has not been requested before.
Here is the code for currentResultDetail
-(NSArray *)currentResultDetail{
NSLog(@"predicate: resultid == %d", [[GameStore defaultStore] currentResultId]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"resultId == %d", [[GameStore defaultStore] currentResultId] ];
NSArray *filtered = [[[GameStore defaultStore] allResults] filteredArrayUsingPredicate:predicate]; // failed here but only if currentResultId has been requested before.
predicate = nil;
[predicate release];
return [[filtered objectAtIndex:0] resultDetails];
}
The above code failed on the following line:
NSArray *filtered = [[[GameStore defaultStore] allResults] filteredArrayUsingPredicate:predicate];
So basically if the user requested currentResultDetail for currentResultId 1,2,3,4,5,6,7 in that order, It works fine. But if they requested 1,2,3,4,5,1 it will crashed.
Any pointer on why this is happening?
Your predicate is returned autorelease. Don’t release it manually or you’ll get a crash when the autoreleasepool tries to release it.