Ser Pounce
Ser Pounce

Reputation: 14571

Problems with NSFetchedResultsController in iOS7?

I am using NSFetchedResultsController to load some data into UITableView. No matter how many objects I save, it only loads the first one. If I delete that one object from the UITableView and reload it, sometimes it will show one or more of the other objects I've saved. It's very unpredictable.

The strange thing is the code I was using worked fine on the iOS6 SDK. I know that the issue is with the NSFetchedResultsController because when I make a fetch request with NSFetchRequest, the data that is returned is correct. Here is the code;

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSError *error;

    if (![[self fetchedResultsController] performFetch:&error])
    {
        exit(-1);
    }
}

- (void)viewDidUnload
{
    self.fetchedResultsController = nil;
}

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

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"HatInfo" inManagedObjectContext:[self.appDelegate managedObjectContext]];
        [fetchRequest setEntity:entity];

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

        [fetchRequest setFetchBatchSize:9];
        NSArray *fetchedObjects = [[self.appDelegate managedObjectContext] executeFetchRequest:fetchRequest error:nil];
        //returns the correct amount of objects
        NSLog(@"FetchedObjects: %lu", (unsigned long)[fetchedObjects count]);

        NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                            managedObjectContext:[self.appDelegate managedObjectContext] sectionNameKeyPath:nil
                                                       cacheName:@"Root"];
        self.fetchedResultsController = theFetchedResultsController;
        _fetchedResultsController.delegate = self;

        return _fetchedResultsController;

    }

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id  sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
    //returns the incorrect amount
    return [sectionInfo numberOfObjects];
}

Upvotes: 0

Views: 341

Answers (2)

vmthiru
vmthiru

Reputation: 1

Add this line before you start fetch :

[NSFetchedResultsController deleteCacheWithName:<< catch name>>];

Upvotes: 0

LombaX
LombaX

Reputation: 17374

This kind of unexpected behavior can happen if you used the same cacheName in other part of core data code (for example: another fetchedResultsController in your app. You can try to change the cache name to something different or nil and see what happens.

Upvotes: 2

Related Questions