获取超过1000个对象时,核心数据崩溃

时间:2017-07-17 17:09:49

标签: ios objective-c core-data nsfetchrequest

当我尝试从Core Data中获取超过1000个NSManagedObjects时,我的应用程序崩溃了这条消息:

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)'

我用来获取用户选择的对象的代码是:

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

有没有更好的方法来获取1000多个不会导致我的应用崩溃的对象?

1 个答案:

答案 0 :(得分:5)

假设uuidStrings包含sID属性的完全匹配项,您应该可以使用此代码替换代码(未经测试):

// remove subPredicatesArray, the loop and compoundPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sID IN %@", uuidStrings];
[request setPredicate:predicate];