Want to sort by name which have one to one relationship using NSSortDescriptor
in NSFetchedResultsController
. Here my code
NSFetchRequest <studentDetail *> *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[studentDetail entityName]];
NSSortDescriptor *sortDescriptorForSchoolName = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(schoolName))
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *sortDescriptorForSchool = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(school))
ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptorForSchool, sortDescriptorForSchoolName]];
[fetchRequest setFetchBatchSize:4];
NSManagedObjectContext *mainContext = [[coreDataManager sharedManager] managedObjectContext];
NSFetchedResultsController *FRC = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:mainContext
sectionNameKeyPath:nil
cacheName:nil];
[FRC setDelegate: self];
NSError *fetchError = nil;
if (![FRC performFetch: &fetchError])
NSLog(@"fetch error: %@", fetchError);
[self setStudentDetailFetchedResultsController: FRC];
Example DB format:
- School and student are entity name.
- School relationship with student one to many.
- Total 4 school. Each school have 4 student.
- Now I want to show all student list with name ascending sort.
In UITableView
I am showing student details. There is a option to sort students are sort by school
entity. In my FRC have studentDetail
. How can I sort those details with FRC?.
From studentDetail
entity have one to one relationship with school
.
Update
school entity
one to many
school1
relationship
->student1
->student2
->student3
->student4
school2
relationship
->student5
->student6
->student7
->student8
school3
relationship
->student9
->student10
->student11
->student12
school4
relationship
->student13
->student14
->student15
->student16
student entity
one to one
student1
relationship
->school1
student2
relationship
->school1
student3
relationship
->school1
student4
relationship
->school1
student5
relationship
->school2
student6
relationship
->school2
student7
relationship
->school2
student8
relationship
->school2
student9
relationship
->school3
student10
relationship
->school3
student11
relationship
->school3
student12
relationship
->school3
student13
relationship
->school4
student14
relationship
->school4
student15
relationship
->school4
student16
relationship
->school4
Got answer from thanks to @RunLoop. But this need few change which I want.
As I mention in my code for to sort NSStringFromSelector(@selector(school))
like, example: [NSSortDescriptor sortDescriptorWithKey[NSString stringWithFormat:@"%K.%K", NSStringFromSelector(@selector(school)), NSStringFromSelector(@selector(schoolName))] ascending:YES]
Instead of [NSSortDescriptor sortDescriptorWithKey:@"school.schoolName" ascending:YES]
If I use this kind of method, In feature if I change any name in entity it's show warning and static one school.schoolName
doesn't. Is that possible?
I tried this method with NSPredicate
that doesn't show any warning. How should I solve this?