如何获取10个最新对象并按降序排序[ios,coredata]

时间:2015-04-16 08:55:24

标签: ios sorting core-data fetch

我可以通过排序descriptorWithKey" ms_createdAt"来获取最新的10个。升序NO,接下来我想重新排列这10个对象升序:是

请告诉我如何

到目前为止这是我的代码

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setFetchLimit:10];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;

ChatInfo *chat = [ChatInfo getInstance];

fetchRequest.entity = [NSEntityDescription entityForName:@"AudioHistory" inManagedObjectContext:context];

// fectch max date first
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(uploadedby == %@ && sentto == %@) || (uploadedby == %@ && sentto == %@)", chat.userid, chat.friendid, chat.friendid, chat.userid];

fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"ms_createdAt" ascending:NO]];

NSFetchedResultsController *firstFetchedResult =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:context sectionNameKeyPath:nil
                                               cacheName:nil];

NSArray *maxDate = firstFetchedResult.fetchedObjects;

NSLog(@"Max Date :%@", maxDate);
// search by channels id

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(uploadedby == %@ && sentto == %@) || (uploadedby == %@ && sentto == %@)", chat.userid, chat.friendid, chat.friendid, chat.userid];

// sort by item text
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"ms_createdAt" ascending:YES]];


NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:maxDate sectionNameKeyPath:nil
                                               cacheName:nil];

self.fetchedResultsController = theFetchedResultsController;
//self.fetchedResultsController = firstFetchedResult;

_fetchedResultsController.delegate = self;

return _fetchedResultsController;

仍然在第二个fetchedResultsController

上出错

0 个答案:

没有答案