问题
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