Caborca87
Caborca87

Reputation: 187

NSFetchedResultsController can't performFetch when sorted attributes aren't all the same

I have a strange issue with an NSFetchedResultsController that can only handle performFetch when all the entity.numbers are the same number (this NSFetchedResultsController sorts the entities using number. This is kind of like a playlist that uses an indexNumber attribute to keep the playlist order.

- (IBAction)addSetPressed {
    int theNumber = [self.nameTextField.text intValue];
    WSet *set = [WSet wSetWithNumber:theNumber inManagedObjectContext:self.workout.managedObjectContext];
    [self.workout addExercisesObject:set];
}

self.nameTextField is in my UITableView's Header. I'll put a number there, like 1 for example, and then I press a UIButton that calls addSetPressed. This code is meant to add an entity to my model and the NSFetchedResultsController detects the change in the model and updates my UITableView.

Here's the issue: When I enter 1 (or any number) in self.nameTextField, I can press the UIButton as many times as I want to and the code works great. But when I change the number in self.nameTextField I get an error:

[__NSCFNumber localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0xb665b80
CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.

I have spent the last 12 hours messing around with my code and researching the issue, but I can't figure it out.

It will be great if you a and tip for me, or can point me towards some literature that can help.

Upvotes: 0

Views: 1308

Answers (2)

Caborca87
Caborca87

Reputation: 187

It looks like NSNumber doesn't like being sent the method localizedCaseInsensitiveCompare:.

I'm going to play around with this to see if I can make the sort value an NSString instead.

If you have any better ideas, let me know!

Upvotes: 0

Caborca87
Caborca87

Reputation: 187

Fixed!

Here's the problem. When I setup my sortDescriptor, I need to select the appropriate selector to compare the type of attribute I'm sorting by.

This is correct since number is handled as an NSNumber

request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES selector:@selector(compare:)]];

I had the selector for NSString before.

Upvotes: 3

Related Questions