获得最近30条记录coredata NSPredicate

时间:2013-01-22 11:48:03

标签: iphone core-data nspredicate

我想从coredata获取最后30条记录,我想使用NSPredicate:目前

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedContext];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((from == %@) AND (to == %@) )OR((from == %@) AND (to == %@))",from, to,to ,from];
[request setPredicate:predicate];


NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedContext
                                        executeFetchRequest:request
                                        error:&error] mutableCopy];

如何添加方法

1 个答案:

答案 0 :(得分:3)

使用sort descriptors将其按正确的顺序排列并将fetch limit设置为30

NSSortDescriptor* fromSortDescriptor = [[NSSortDescriptor alloc]
                            initWithKey:@"from" ascending:YES];
[request setSortDescriptors:@[fromSortDescriptor]];
[request setFetchLimit:30];