I have one, many parent child Book ↔> Lorrow
Book
Borrow
- borrowDate
- note
- book (attitude)
and I want to sort the Book object into NSFetchedResultsControllerusing the max NSDatechild object to show the most recently borrowed book.
How can i do this? I tried using my own method like this using Category on Book
- (NSArray *)borrowSortedByborrowedDate
{
NSSortDescriptor *sortByCreatedDate = [NSSortDescriptor sortDescriptorWithKey:@"borrowDate" ascending:NO];
NSArray *sortedArray = [self.histories sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByCreatedDate]];
return sortedArray;
}
- (NSDate *)recentBorrowDate
{
Borrow *borrow = [[self borrowSortedByborrowedDate] objectAtIndex:0];
return borrow.borrowDate;
}
and put it in sortDescriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"recentBorrowDate" ascending:YES];
but it didn’t work.
Here is my request (not working)
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"recentBorrowDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
Expected Result
- Book name1 (loanDate today)
- Write name2 (loanDate yesterday)
- Book name3 (loanDate many days ago)
source
share