NSFetchedResultsController Fetch Request - Update Predicate and UITableView

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, , , , - ( , ).

, , . , . , ,

+3
3

NSFetchedResultsController . List, List :

NSSet *tasks = [[self list] valueForKey:@"tasks"];
NSSortDescriptor *sort = ...;
NSArray *sorted = [[tasks allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
[self setTasks:sorted];

UITableviewDatasource tasks.

+1

, ,

detailViewController.list.task

( , ) NSSet , List.

0

, , NSFetchedResultsController ( ) UITableView..

After I published it, I decided to try my code. And you know what? It works, with only a small addition:

    [self.tableView reloadData];

Perhaps you can go back to the source code and confirm.

Sincerely.

0
source

Source: https://habr.com/ru/post/1738748/


All Articles