Search code examples
ioscore-datarestkit

NSFetchedResultsController not firing controllerDidChangeContent


My current setup:

I intend to use a UISegmentedControl to switch between the data i'd like to show in a tableview. I have been using restkit for all restful interactions to the webservice. The data is stored in a coredata sqlite store. I have setup a NSFetchedResultsController in the following fashion.

-(void)setupFetchedResultsControllerWithFetchRequest:(NSFetchRequest *)fetchRequest{

    self.fetchedResultsController=nil;

    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"address" ascending:YES];
    fetchRequest.sortDescriptors = @[descriptor];
    NSError *error = nil;
    // Setup fetched results
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                        managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
    [self.fetchedResultsController setDelegate:self];
    BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error];
    // NSAssert([[self.fetchedResultsController fetchedObjects] count], @"Seeding didn't work...");
    if (! fetchSuccessful) {
        ShowAlertWithError(error);
    }

}

and this is how i have written fetch requests to pass into the fetchedresultscontroller.

-(NSFetchRequest *)prepareFetchRequestForGlobalAtm{

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"AtmBranches"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"branchType='0'"];
    [fetchRequest setPredicate:predicate];
    return fetchRequest;

}

I have mated them together in the segmentedControlToggledMethod appropriately.

[self setupFetchedResultsControllerWithFetchRequest:[self prepareFetchRequestForGlobalAtm]];

//Restkit Call
[self loadGlobalAtms]; 

this is my delegate method for NSFetchedResultsController.

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.locationsTableView reloadData];
}

My Logic here is that every toggle should fire methods to setup the NSFetchedResultsController according to the values that have been passed. I can see clearly in the logs that i have values coming in from the webservice and are being cached, but the tableview never populates itself.


Solution

  • You havn't change the data, so you can't receive the notification.

    When you performFetch with self.fetchedResultsController, you have already got all the objects.

    You can receive the delegate's callback when you insert/update/delete in the mainManagedObjectContext or its child ManagedObjectContext

    You should do like this

    [self.fetchedResultsController setDelegate:self];
    BOOL fetchSuccessful = [self.fetchedResultsController performFetch:&error];
    // NSAssert([[self.fetchedResultsController fetchedObjects] count], @"Seeding didn't work...");
    if (! fetchSuccessful) {
        ShowAlertWithError(error);
    }
    [self.locationsTableView reloadData];
    

    And keep the logic when delegate callback