EmilyP
EmilyP

Reputation: 118

Core data crash when fetching over 1000 objects

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

Answers (1)

danh
danh

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

Related Questions