Sort NSFetchedResultsController using a child attribute

I have one, many parent child Book ↔> Lorrow

Book

  • name
  • borrows (ratio)

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)
+3
source share
3 answers

, . (, , ...)

book.lastBorrowDate .

// Create new Borrow and set Book
Borrow *newBorrow = (Borrow *)[NSEntityDescription insertNewObjectForEntityForName:@"Borrow" inManagedObjectContext:self.managedObjectContext];
newBorrow.book = theBook;
newBorrow.borrowDate = [NSDate date];
newBorrow.note = theNote;

// Now also update our book lastBorrowDate tracking property
theBook.lastBorrowDate = [NSDate date];

// Save all our changes
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    // Something went horribly wrong...
    NSLog(@"Unresolved error: %@, %@, %@", error, [error userInfo],[error localizedDescription]);
    abort(); // Fail
}

, , ...

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastBorrowDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];
0

, lastBorrowedDate Books. , MOM, . .

0

. : NSSortDescriptor

, , , . , , . , , fetchResults, . , . , , :

@interface Book(category) {}
@end
@implementation Book(category) 
- (NSDate*)getMostRecentDate {
  NSDate* mostRecentDate = [NSDate dateWithTimeIntervalSince1970:0];
  for(Borrow* b in [self borrows]) {
    mostRecentDate = [mostRecentDate laterDate:[b borrowDate]];
  }
  return mostRecentDate;
}
@end

@interface SortDescriptor : NSSortDescriptor {}
@end
@implementation SortDescriptor
- (id)copyWithZone:(NSZone*)zone
{
    return [[[self class] alloc] initWithKey:[self key] ascending:[self ascending] selector:[self selector]];
}
- (NSComparisonResult)compareObject:(id)object1 toObject:(id)object2
{
    return [[(Book*)object1 getMostRecentDate] compare:[(Book*)object2 getMostRecentDate]];
}
@end

, . , , , NSArray.

0

All Articles