2017-07-17 46 views
1

当我试图从核心数据获取超过1000个NSManagedObjects,我的应用程序崩溃与此消息:获取超过1000时,核心数据碰撞对象

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

直到我反对它,我也没有! – EmilyP

回答

5

假设uuidStrings包含了sID属性完全匹配项,你应该能够用这种(未经测试),以替换代码:

// remove subPredicatesArray, the loop and compoundPredicate 

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

这使它工作并运行得更快!我不确定我是如何使用复合谓词来代替你所建议的谓词的,但是这固定了一切! – EmilyP