I implement a UITableViewController in conjunction with an NSFetchedResultsController. When an instance of the UITableViewController is created, an NSFetchedResultsController (almost the same as the CoreDataBooks example) is created with another predicate based on the user selection made on the previous controller.
Sometimes I set the predicate as follows:
predicate = [NSPredicate predicateWithFormat:@"container = %@", aManagedObject];
sometimes this way:
predicate = [NSPredicate predicateWithFormat:@"container IN (%@)", aNSMutableSetOfObjectsID];
Both predicates work just fine because they select all of my objects correctly as expected. The problem occurs when I try to insert a new managed entity into the list when the second predicate is set. Infact in this case, the FRC delegate methods are never called, so the tableview is not updated automatically.
I am sure that the new object is correctly added to the same context that FRC uses, and that its container relation is set so that the delegate needs to be launched, but it is not!
What I tried:
, , , , UITableViewController , , .
, , - FRC :
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
exit(-1);
}
[self.tableView reloadData];
.
:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = realEstate.managedObjectContext;
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext]];
[fetchRequest setPredicate:[self getBasePredicate]];
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:nameDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[nameDescriptor release];
[sortDescriptors release];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"sectionIndex"
cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
[aFetchedResultsController release];
[fetchRequest release];
}
return fetchedResultsController;
}
- (NSPredicate *)getBasePredicate {
NSPredicate *predicate = nil;
if ([[managedObject valueForKey:@"subcontainersCount"] intValue] == 0)
predicate = [NSPredicate predicateWithFormat:@"container = %@", managedObject];
else {
predicate = [NSPredicate predicateWithFormat:@"container IN (%@)", [Container allDescendantsObjectID:(Container *)managedObject includeSource:YES]];
}
return predicate;
}
- (void)addNewItem {
Item *newItem = (Item *)[NSEntityDescription insertNewObjectForEntityForName:@"Item"
inManagedObjectContext:realEstate.managedObjectContext];
newItem.name = @"my new item";
newItem.container = aContainerManagedObject;
NSError *error = nil;
if (![realEstate.managedObjectContext save:&error]) {
abort();
}
}
NSFetchedResultsControllerDelegate, , "More iPhone 3 Development". (. 30-31, 35).
, .