Search code examples
iosnsfetchedresultscontrollernsfetchrequest

NSFetchController fetch with other predicate when 1ste one fails


I'm having a fetchrequest. Now I need to get "tracked" points from the database but when there are no tracked points they all need to be retrieved.

How can I do that? I now set a predicate on my fetchcontroller but when it doesn't find any tracked points it just doesn't show anything. So where can I hook up on this and then say retrieve everything...

Is there a way to listen that there is nothing found or how is this done?

EDIT:

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rank" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]];
    [fetchRequest setEntity:entity];

/*    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"Auto"];
    [fetchRequest setPredicate:predicate];*/

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:@[sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
            [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:nil
                                                           cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

Solution

  • Thanks to Larme this is the solution:

    - (NSFetchedResultsController *)fetchedResultsController {
    
        if (_fetchedResultsController != nil) {
            return _fetchedResultsController;
        }
    
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rank" inManagedObjectContext:[NSManagedObjectContext MR_defaultContext]];
        [fetchRequest setEntity:entity];
    
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", @"DON"];
        [fetchRequest setPredicate:predicate];
    
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
        [fetchRequest setSortDescriptors:@[sort]];
    
        [fetchRequest setFetchBatchSize:20];
    
        NSFetchedResultsController *theFetchedResultsController =
                [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                    managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:nil
                                                               cacheName:nil];
        self.fetchedResultsController = theFetchedResultsController;
        _fetchedResultsController.delegate = self;
    
        if ([[_fetchedResultsController fetchedObjects] count] == 0)
        {
            NSLog(@"0 found");
            [fetchRequest setPredicate:nil];
        }
    
        return _fetchedResultsController;
    
    }