Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refresh/reload to update changes to my predicate & thus fetch request?

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:'
like image 792
msec Avatar asked Oct 17 '11 23:10

msec


2 Answers

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?

like image 125
Adam Eberbach Avatar answered Nov 09 '22 02:11

Adam Eberbach


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.

like image 41
Pete Avatar answered Nov 09 '22 00:11

Pete