【发布时间】:2012-05-14 05:54:22
【问题描述】:
我有一对多的父子实体书>借
书
- 姓名
- 借(关系)
借
- 借用日期
- 注意
- 书(关系)
我想使用子实体的最大NSDate 对NSFetchedResultsController 中的图书实体进行排序,以显示最近借阅的图书。
我该怎么做?我尝试使用我自己的方法,例如使用 Book 上的 Category
- (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;
}
并将其放入sortDescriptor
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"recentBorrowDate" ascending:YES];
但是没有用。
这是我获取的请求(无效)
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];
我的预期结果是
- 书名1(今天借用日期)
- 书名2(昨天借用日期)
- 书名3(borrowDate 许多天前)
【问题讨论】:
-
您能提供获取请求吗?那么您是否针对
Book执行请求?最后,History是什么,note 又是什么?谢谢。 -
是的 fetch 请求在没有谓词的情况下针对 Book 执行。我想使用最近借书的 NSDate 对其进行排序。注意是借用的一种属性。
-
您能提供您正在使用的获取请求吗?您想订购从最近日期到最后一本的书籍吗?谢谢。
-
我在帖子中添加了获取请求,但没有成功。
-
@art 我目前正试图弄清楚如何做同样的事情
标签: objective-c core-data nsfetchedresultscontroller nssortdescriptor