I’m working on App with coreData and i get an error that i can’t understand: In a custom init whether i define NSFetchedResultController with “self.controller = new_controller” all works correctly, if i define only “controller = new_controller” (without self), error “The NSManagedObject has been invalidated” occur after 2 loops for the situation described in this image:

Application parts interested in this error is here described:
-
A Table with a list of Months (Class MonthListVC, step 1 in the image). Where
sections describe Month (like April
2011) and the unique Row for any
section present data (that are sum
from transactions happened during the
month). -
Selecting one of these Row a next
Table is pushed in navigation
controller (Class WeekTVC, step 2 in the image), this table presents
detail for every weeks for selected month. Here section title has format “week beginning – week end”, and the unique row for the section is a sum of transactions for the week. -
A NSManagedObjectContext is passed via a custom init method (check the code)
-
every data for these tables are managed with
NSFetchedResultController.
Here the code for selection on first Table (MonthListVC)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:indexPath.section];
// get the first object for this section and get its date.
NSDate *datepoint = [[[sectionInfo objects]objectAtIndex:0] date];
//Create next Controller and push it into Navigation
WeeksTVC *weeksTVC = (WeeksTVC*)[[WeeksTVC alloc] initWithContext:self.context datepoint:datepoint withTitle: [sectionInfo name] ];
[self.navigationController pushViewController:weeksTVC animated:YES];
[weeksTVC release];
}
And here the code for WeeksTVC custom init method, where i define “self.controller = …” and where i get the problem whether i write only “controller = …”.
“controller” is a synthesized property.
-(WeeksTVC *)initWithContext:(NSManagedObjectContext *)n_context datepoint:(NSDate*)n_datePoint withTitle:(NSString*) stringTitle{
self = [self init];
if(self !=nil){
self.title = [NSString stringWithFormat:@"History (%@)",stringTitle];
//Back button setup
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Back";
self.navigationItem.backBarButtonItem = barButton;
//datePoint = n_datePoint;
self.context = n_context;
//Get Begin and End week information for this datePoint
NSDate *startDay = [[CommonHelper weekInfoFromData:[CommonHelper firstDayOfMonthForDate:n_datePoint]] objectForKey:@"begin"];
NSDate *endDay = [[CommonHelper weekInfoFromData:[CommonHelper lastDayOfMonthForDate:n_datePoint]] objectForKey:@"end"];
//Manage Request
NSFetchRequest *request = [[NSFetchRequest alloc]init];
request.entity = [NSEntityDescription entityForName:@"Transactions" inManagedObjectContext:self.context];
request.predicate = [NSPredicate predicateWithFormat:@"(date >= %@) && (date <= %@)",startDay,endDay];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
NSArray *sortDescs = [NSArray arrayWithObjects:sort,nil];
request.sortDescriptors = sortDescs;
"HERE THE PROBLEM WITH SELF***********************************************"
//Manage NSResultFetchedController
self.controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:@"byWeek" cacheName:nil];
self.controller.delegate = self;
NSError *error = nil;
//Fetch
[self.controller performFetch:&error];
"END OF PROBLEM***********************************************************"
[request release];
[barButton release];
}
return self;
}
I report also dealloc method for WeeksTVC, i found that if i don’t release context and controller in this method… no error occur also if i write in init method “controller = ” and not “self.controller = “
- (void)dealloc {
//[self.datePoint release];
[self.controller release];
[self.context release];
[super dealloc];
}
Why self.controller and controller are so different in this case ???
In init method i ever write ivar without self -.-‘
In custo init method for WeeksTVC i wrote
and not
This cause the problem… but i’m curious to understand what happen exactly…