PanicDev
PanicDev

Reputation: 325

Core data sorting not working

I am trying to get a core data model sorted by company I am currently getting an error stating that I have an unknown receiver device. what am I doing wrong?

EDIT: Here is the error I am receiving:

enter image description here

Here is some code-

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];


    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    
    

    
    NSArray *sortDescriptors = @[
                                 [NSSortDescriptor sortDescriptorWithKey:@"company" ascending:YES]
                                 ];
    
    NSArray *sortedPeople = [self.devices sortedArrayUsingDescriptors:sortDescriptors];
    NSLog(@"%@", sortedPeople);
    



    
    
    [self.tableView reloadData];
}

Upvotes: 0

Views: 147

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70966

From your comments, it seems the line at fault is this one:

NSArray *sortedPeople = [Device sortedArrayUsingDescriptors:sortDescriptors];

And the problem seems to be that Device is unknown to the compiler. In other words, you're saying that you want to sort something, but the compiler is unable to figure out what it is you want to sort.

It looks like Device is a Core Data entity type, but from your question I can't be sure if you actually have a class named Device. You used @"Device" in the fetch request, but the compiler doesn't try to resolve strings as symbols. So it's impossible to be certain of what you need to fix. Based on your description of what you're trying to do, you probably want to change that line to

NSArray *sortedPeople = [self.devices sortedArrayUsingDescriptors:sortDescriptors];

You also mentioned that you got crashes when doing that. But after much back and forth in comments you still haven't given any clue what error(s) or other messages you might be getting when the crash occurs. As a result, who knows? Maybe the Device entity doesn't have an attribute named company, and the sort fails because of that. Maybe something else. If you're unwilling to provide crucial details of your problem even after repeated requests, all anyone can do is try to guess.

Upvotes: 0

Related Questions