0

我想从核心数据中获取最后 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];

如何添加方法

4

1 回答 1

3

使用排序描述符将其放入正确的顺序并将获取限制设置为 30

NSSortDescriptor* fromSortDescriptor = [[NSSortDescriptor alloc]
                            initWithKey:@"from" ascending:YES];
[request setSortDescriptors:@[fromSortDescriptor]];
[request setFetchLimit:30];
于 2013-01-22T12:04:33.660 回答