Search code examples
ioscore-datansfetchrequest

Can I modify the fetchRequest after creating the FetchResultController Object.?


Sorry If I am not able to explain it perfectly. I have a fetchResultController getter.

As below-

- (NSFetchedResultsController *)fetchedResultsController
{
        if (_fetchedResultsController != nil) {
                return _fetchedResultsController;

        }

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName: self.entityName inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];

        // Set the batch size to a suitable number.
        [fetchRequest setFetchBatchSize:20];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:keyColumnNamePid ascending:NO];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

        [fetchRequest setSortDescriptors:sortDescriptors];


NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"myCache"];
    self.fetchedResultsController = aFetchedResultsController;

        self.fetchedResultsController.delegate = self;

        // Perform Fetch on Result Controller
 NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }        
            return _fetchedResultsController;
}

Now I want to set some predicate and properties of fetchRequest.(Only if it required.) I mean I made another method Like this-

-(NSFetchedResultsController *) fetchResultsControllerWithPredicate:(NSPredicate *) predicate{

    [self.fetchedResultsController.fetchRequest setPredicate:predicate];

    [NSFetchedResultsController deleteCacheWithName:@"myCache"];


    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

Now When I am printing it, It is returning the _PFBatchFaultingArray as Below:-

"<LibraryItem: 0xdc22150> (entity: LibraryItem; id: 0xdc208b0 <x-coredata://A1159A80-CA8B-4443-B96C-582BF0DC0290/LibraryItem/p332> ; data: <fault>)",
    "<LibraryItem: 0xdc224d0> (entity: LibraryItem; id: 0xdc1c590 <x-coredata://A1159A80-CA8B-4443-B96C-582BF0DC0290/LibraryItem/p290> ; data: <fault>)",
    "<LibraryItem: 0xdc22510> (entity: LibraryItem; id: 0xdc1c5a0 <x-coredata://A1159A80-CA8B-4443-B96C-582BF0DC0290/LibraryItem/p224> ; data:<fault>)"

Problem is " data:'<'fault'>' ".

What is problem with this code? Am I doing something wrong?

Thanks for your help.


Solution

  • I realize the answer after wasting a complete day. YES We can modify.

    But once you access the data you will find correct data. So don't worry if is showing Faulty data while printing.

    **When you just trying to print the values it will print a data : fault.
    Because CoreData fetch data on Demand, means while you are printing it, it is partial Data.
    When you access (you demand) the value, you find the complete data.**