In the iPhone Core Data application, I configured it in the settings for viewing master data.
A master view is a UITableView listing the objects of an object List. A List entity has a to-many relationship with an object Task(called "tasks"), and a Task object has feedback with an object Listcalled "list".
When an object is selected in the main view List, I want the part view (another UITableView) to display the objects Taskcorresponding to that object List. What i have done so far:
In the detail view controller, I declared a property for the object List:
@property (nonatomic, retain) List *list;
Then in the main view controller, I use this table delegate method to set the property of the Listdetailed view controller when selecting a list:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailViewController.list = (List*)selectedObject;
}
Then I overridden the setter for the property Listin the detail view controller as follows:
- (void)setList:(List*)newList
{
if (list != newList) {
[list release];
list = [newList retain];
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"(list == %@)", list];
[NSFetchedResultsController deleteCacheWithName:@"Root"];
[[[self fetchedResultsController] fetchRequest] setPredicate:newPredicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
What I am doing here is setting a predicate from the results to filter objects, so I only get those that belong to the selected object List. The FetchedResultsController receiver for the detail view controller is as follows:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FALSEPREDICATE"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
}
return fetchedResultsController;
}
Core Data, , , - , false, , , , - ( , ).
, , . , . , ,