2017-07-17 77 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