views:

357

answers:

3

I have an Core Data Entity with a number of attributes, which include amount(float), categoryTotal(float) and category(string)

The initial ViewController uses a FethchedResultsController to retrieve the entities, and sorts them based on the category and then the categoryTotal. No problems so far.

NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(dateStamp >= %@) AND (dateStamp =< %@)", startDate, endDate];
[request setPredicate:predicate];

NSSortDescriptor *sortByCategory = [[NSSortDescriptor alloc] initWithKey:@"category" ascending:sortOrder];
NSSortDescriptor *sortByTotals = [[NSSortDescriptor alloc] initWithKey:@"categoryTotal" ascending:sortOrder];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByTotals, sortByCategory, nil];
[request setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:@"category" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

On selecting a row (tableView:didSelectRowAtIndexPath), another view controller is loaded that allows editing of the amount field for the selected entity.

Before returning to the first view, categoryTotal is updated by the new ‘amount’. The problem comes when returning to the first view controller, the app bombs with

*Serious application error. Exception was caught during Core Data change processing: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). with userInfo (null) Program received signal: “EXC_BAD_ACCESS”.*

This seems to be courtesy of NSSortDescriptor *sortByTotals = [[NSSortDescriptor alloc] initWithKey:@"categoryTotal" ascending:sortOrder]; If I remove this everything works as expected, but obviously without the sorting I want.

I'm guessing this is to do with the sorting order changing due to categoryTotal changing (deletion / insertion) but can't find away fix this. I've verified that values are being modified correctly in the second view, so it appears down to the fetchedResultsController being confused. If the categoryAmount is changed to one that does not change the sort order, then no error is generated

I'm not physically changing (ie deleting) the number of items the fetchedResultsController is returning ... the only other issue I can find that seem to generate this error

Any ideas would be most welcome

Thanks, AJ

A: 

If categoryTotal is a transient property you cannot sort by this property using a fetch request. You will need to sort the fetchedObjects manually (by code) into a second array which you can use in the table.

Felix
categoryTotal is not transient. Therefore by my reckoning the the controller should sort the fetch results
AJ
A: 

The ordering of your managed objects via sectionNameKeyPath MUST match the ordering of your managed objects via your primary sort descriptor. Since you are using "category" as your sectionNameKeyPath and yet using "categoryTotal" as your primary sort descriptor, it will not be universally true that the sectionNameKeyPaths have the same ordering as the primary sort descriptor. The reason that removing sortByTotals fixes the problem is that "category" then becomes both the sectionNameKeyPath and the primary sort descriptor, and thus they both have the same ordering.

glorifiedHacker
A: 

Try changing your sort in your NSFetchedResultsController to first sort by category and then by categoryTotal. This should resolve the issue. If it does not, please post your NSFetchedResultsController delegate methods.

Marcus S. Zarra