Reputation: 171
I am trying to use NSSortDescriptor in Core Data to fetch my records. Array of modal-objects doesn't get affected by sort descriptor. It gives records in same order.Here is my code:
NSManagedObjectContext *moc=[self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"To_Do" inManagedObjectContext:moc];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"repeatDate" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[sortDescriptor release];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
NSError * error = nil;
NSArray *arrEntity = nil;
arrEntity=[moc executeFetchRequest:fetchRequest error:&error];
Upvotes: 2
Views: 2418
Reputation: 53
I had same problem if I had done like this
fetchRequest.sortDescriptors.append("key")
But if I use
fetchRequest.sortDescriptors = ["key"]
Then it works fine
Upvotes: 0
Reputation: 64428
The code looks fine and should work. Since it does not there a couple of possible causes.
Upvotes: 4