如何从数据库中获取一系列项目

时间:2011-12-29 19:15:29

标签: ios core-data

如何在我的SQLite表中获取项目7 000到7 999,大约有10万个项目?

正常的提取回归到很多工作,我宁可一点一点地获取它:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Log"
                      inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&err];
// here fetchedObjects holds around 100 000 rows
  • 有没有办法在executeFetchRequest:fetchRequest
  • 中指定范围
  • 我是否必须使用SQLite API?
  • 这是个好主意吗?

1 个答案:

答案 0 :(得分:2)

使用fetchLimit上的fetchOffsetNSFetchRequest属性来获取所选范围内的对象。

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.fetchLimit = 1000;
fetchRequest.fetchOffset = 7000;
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Log"
                      inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&err];