I have a method in a UIViewController of my iPhone application (inside a UINavigationController) that is called whenever a row is selected in the table in the ViewController's view. In this method, I access array of "Dream"'s stored in an instance field dreamsArray, which contains NSManagedObjects from my database. I can access objects from this array in other methods, but it seems that whenever I try to retrieve or modify retrieved objects from this array in this particular method, the program crashes.
Here is how dreamsArray is created:
dreamsArray = [[NSMutableArray alloc] init];
[self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Dream" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; [sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if ( mutableFetchResults == nil )
NSLog(@"oh noes! no fetch results DreamsTabController:45");
dreamsArray = [mutableFetchResults mutableCopy];
[mutableFetchResults release];
[request release];
An instance in which querying dreamsArray and its objects works:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if ( cell == nil )
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
Dream *dream = (Dream *)[dreamsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dream title];
cell.detailTextLabel.text = @"foo!";
[dream release];
return cell;
}
And the method that has all the problems:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Dream *dream = (Dream *)[dreamsArray objectAtIndex:indexPath.row];
// BOOM - crashes right here
EditDreamController *edit = [[EditDreamController alloc] initWithNibName:@"EditDream" bundle:nil];
edit.dream = [[NSArray alloc] initWithObjects:dream.dreamContent, nil];
[navigationController pushViewController:edit animated:YES];
[dream release];
[edit release];
}
The app crashes immediately after dreamsArray is queried.
Even calling a simple NSLog(@"%@", dream.title)
in this method causes a crash. What could be going wrong here?