How to fetch a range of items from the database

北战南征 提交于 2020-01-16 09:04:04

问题


How do I fetch, say, item 7 000 to 7 999 in my SQLite table with around 100 000 items?

The normal fetch returns to much to work with, and I rather fetch it little by little:

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
  • Is there a way to specify a range with in executeFetchRequest:fetchRequest?
  • Do I have to use the SQLite API?
  • Is it a good idea?

回答1:


Use the fetchLimit and fetchOffset properties on NSFetchRequest to fetch an object in a selected range.

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];


来源:https://stackoverflow.com/questions/8672349/how-to-fetch-a-range-of-items-from-the-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!