7

Hi I currently have a table view which is being filled via Core Data.

I am limiting the results using NSPredicate so that only items with the same OrderNumber are displayed in the tableView.

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                @"orderNumber == %@",     orderNumberLabel.text];
NSLog(@"predicate is: %@",predicate);
[fetchRequest setPredicate:predicate];

Once the form for entering an order has been completed a button is pressed [save order] and the orderNumber is then incremented by one, as well as the orderNumberLabel.

What I expect to happen is that the tableView should then be empty as the new orderNumber has no entries in the sql database. However this does not occur and calling [myTableView reloadData]; does not resolve it either.

However, if I quit the project and re-run it I have the new orderNumber and an empty tableView. So there is something happening at run-time with the fetch request and predicate being set that I need to try & replicate with a call in my saveOrderButtonPressed method. Only problem is that I don't know how to do this, could someone help please?

Occassionally I have also been getting the following error after quitting and re-running the app after entering orders. It is related to me changing the orderNumber but I think it will disappear once I am updating the predicate/fetchRequest. I also need some guidance on this, will I need to disable caching?

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason:'FATAL ERROR: The persistent cache of section information does not match 
the current configuration.  You have illegally mutated the NSFetchedResultsController's
fetch request, its predicate, or its sort descriptor without either disabling 
caching or using +deleteCacheWithName:'

4 Answers 4

7

You are right to use a NSFetchedResultsController. It is made exactly for the use you describe. As the error message says, you can set the cache of the NSFetchedResultsController to nil to completely disable caching - not a big performance hit when you have so few items. Or you can use +deleteCacheWithName: with the name you assigned to the cache. Only then can you change the fetch request (e.g. the predicate) and then you must do performFetch: again. Hopefully you have the delegate hooked up so your table view is automatically refreshed. You don't say you are doing any of the things that will cause problems (changing predicate, sort order etc.) - are you sure you're not?

3
  • Yes I have declared the delegate in the header file and also have the methods for controller will&did change content. It reloads data fine when I add & delete items to the order. I don't think I change the predicate, the predicate stays the same and I just change the value that its pointing to, the value in the OrderNumberLabel. I also have a sort order in place according to the timestamp. I have set the cache to nil now and that has done the trick. How would I go about implementing the performFetch:?
    – msec
    Oct 18, 2011 at 1:47
  • With your fetched results controller, performFetch: is what makes it go off and get information from Core Data. Have you had a close look at developer.apple.com/library/ios/#documentation/CoreData/…? Oct 18, 2011 at 2:29
  • Thanks Adam, I had read through it quite a few times but I am new to programming and it takes a while to sink in, sometimes I'm not sure if I'm taking the right approach which is why your input has been great! I found the problem and it works now! At the start of my code for the NSFetchedResultsController I had if (fetchedResultsController != nil) { return fetchedResultsController; } This meant that the rest of the code wouldn't run to update the predicate :) Thanks once again for your input
    – msec
    Oct 18, 2011 at 4:08
2

I found Adam's reply helpful - thanks. I had a similar problem where I am updating using NSFetchedResultsControllers with a common context. The data can be updated (changed - not added or removed) from either of a couple of table views but the change was not being reflected in the other view.

A fix to this was to call performFetch AND reloadData on viewWillAppear. Not very efficient I know - I need to add in a mechanism to only do this when the data has actually been updated.

0

You want to use an NSFetchedResultsController to populate your tableView.

You can implement the NSFetchedResultsControllerDelegate methods to specify how the tableView should update in response to various events.

NSFetchedResultsController monitors your fetchRequest in the background and calls its delegate methods in response to events.

0

I had a similar situation where I wanted to update the table with a different predicate.

[tableView realoadData]

alone didn't do it, but thanks to the hint about the

(fetchedResultsController != nil) { return fetchedResultsController; } 

I got it working by clearing the fetchResultsController before reloading in my case

_fetchedResultsController=nil;
[tableView reloadData];

Thanks msek!

1
  • Probably You also have to [fetchedResultsController release] before nil-ing it. May 19, 2013 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.