-1

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:

  1. School and student are entity name.
  2. School relationship with student one to many.
  3. Total 4 school. Each school have 4 student.
  4. 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

enter image description here

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?

Community
  • 1
  • 1
Mathi Arasan
  • 869
  • 2
  • 10
  • 32

1 Answers1

0

The following code will fetch the students, ordered by schoolName then by studentName:

NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
NSSortDescriptor *schoolNameSD = [NSSortDescriptor sortDescriptorWithKey:@"school.schoolName" ascending:YES];
NSSortDescriptor *studentNameSD = [NSSortDescriptor sortDescriptorWithKey:@"studentName" ascending:YES];
fr.sortDescriptors = @[schoolNameSD, studentNameSD];

NSManagedObjectContext *moc = [[coreDataManager sharedManager] managedObjectContext];


NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fr
                                                                          managedObjectContext:moc
                                                                            sectionNameKeyPath:nil
                                                                                     cacheName:nil];
[frc performFetch:nil];

If you only want to sort by studentName, simply remove the schoolNameSD and ask frc to perform the fetch again.

RunLoop
  • 20,288
  • 21
  • 96
  • 151
  • Thanks @RunLoop. It worked!!! actually I tried this before, at that time this method not working. But I want to use like this `NSStringFromSelector(@selector(schoolName))` format. I tried to use with `NSString`and I got warning. `[NSString stringWithFormat:@"%K.%K", NSStringFromSelector(@selector(school)), NSStringFromSelector(@selector(schoolName))]` warning `Invalid conversion specifier K`. 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 that's why, can you help to solve this? – Mathi Arasan Mar 22 '17 at 07:49
  • Hi, glad I could help. Please mark my answer as correct by clicking on the ✓. You should not specify the sort key by using a selector. If you want to perform a different sort, assign a new sort descriptor to the frc and ask it to resort. – RunLoop Mar 22 '17 at 07:56
  • No, Not another sort same one. I will mark as correct one but still I need exact formation which I am using another sort, also I am post question like this format too. `[NSSortDescriptor sortDescriptorWithKey[NSString stringWithFormat:@"%K.%K", NSStringFromSelector(@selector(school)), NSStringFromSelector(@selector(schoolName))] ascending:YES]` Instead of `[NSSortDescriptor sortDescriptorWithKey:@"school.schoolName" ascending:YES]` this one give Waring `Invalid conversion specifier K`. I tried to find out myself (before post this question), but I couldn't get what I want. – Mathi Arasan Mar 22 '17 at 09:16
  • It is expected that when a question has been answered it is marked as correct. Follow-up questions should be put as a new question and not an additional question in the comments. – RunLoop Mar 22 '17 at 12:02
  • I want to follow coding structure that's why I still waiting for sort with `NSStringFromSelector`, and this is not new question it's just like sub question. I will update question. – Mathi Arasan Mar 22 '17 at 12:24