Reputation: 118
When I try to fetch more than 1000 NSManagedObjects from Core Data, my app crashes with this message:
error: (1) I/O error for database at .../Documents/Stores/Model.sqlite.
SQLite error code:1, 'Expression tree is too large (maximum depth 1000)'
CoreData: error: (1) I/O error for database at .../Documents/Stores/Model.sqlite.
SQLite error code:1, 'Expression tree is too large (maximum depth 1000)'
The code I use to fetch the objects selected by the user is this:
NSManagedObjectContext *context = cdh.context;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Spot" inManagedObjectContext:context];
NSError *error = nil;
[request setEntity:entity];
request.includesPropertyValues = NO;
NSMutableArray *subPredicatesArray = [NSMutableArray array];
for (NSString *string in uuidStrings)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", @"sID", string];
[subPredicatesArray addObject:predicate];
}
NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicatesArray];
[request setPredicate:compoundPredicate];
NSArray *fetchedObjects = [context executeFetchRequest:request error:&error];
Is there a better way to fetch 1000+ objects that won't cause my app to crash?
Upvotes: 1
Views: 290
Reputation: 62676
Assuming the uuidStrings
contain exact matches for the sID
attribute, you should be able to replace the code with this (untested):
// remove subPredicatesArray, the loop and compoundPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sID IN %@", uuidStrings];
[request setPredicate:predicate];
Upvotes: 5