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

Core data crash when fetching over 1000 objects

当我尝试从 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 多个不会导致我的应用程序崩溃的对象?

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

// remove subPredicatesArray, the loop and compoundPredicate

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