2014-09-29 48 views
1

我需要一些幫助,這方法:核心數據:表達式樹太大

// deletes all points in database except points with given identifiers 
- (void)deleteAllPointsExcept:(NSArray *)safeIdentifiers save:(BOOL)save { 
    // create request to fetch all 'doomed' points 
    NSManagedObjectContext *context = [[TSAppDelegate delegate] managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MapPoint"]; 
    NSMutableArray *subpredicates = [NSMutableArray array]; 

    for (NSNumber *identifier in safeIdentifiers) { 
     NSPredicate *pred = [NSPredicate predicateWithFormat:@"pid!=%@", identifier]; 
     [subpredicates addObject:pred]; 
    } 
    fetchRequest.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates]; 

    // fetch 'em & destroy 'em 
    NSError *error; 
    NSArray *points = [context executeFetchRequest:fetchRequest error:&error]; 

    for (TSMapPoint *point in points) { 
     [context deleteObject:point]; 
    } 

    // save if requested 
    if (save) { 
     [[[TSAppDelegate delegate] managedObjectContext] save:NULL]; 
    } 
} 

它應該從我的核心數據NSManagedObjectContext清除不需要的MapPoints。它工作正常,但有一天,我收到了這樣的信息:

CoreData: error: (1) I/O error for database at ... SQLite error code:1, 'Expression tree is too large (maximum depth 1000)' 

我GOOGLE了,當謂語過長就像WHERE id=1 OR id=2 OR id=3 ...但我不知道如何解決這個發生這種情況。有人有想法嗎?

在此先感謝, 皮特。

回答

1

你的謂語應簡化到像下面

[NSPredicate predicateWithFormat:@"NOT (pid IN %@)", safeIdentifiers]; 
+0

哇,這看起來很優雅。 :)你是否肯定它可以在iOS上的Core Data下使用SQLite? – 2014-09-29 17:30:58

+1

是的 - 有一個可怕的很多強大的謂詞,比這更復雜的工作與SQLite完美 – 2014-09-29 17:32:10

+0

嗯,我已經試過你的方法,並得到:'NSInvalidArgumentException - 未實現SQL生成謂詞︰(pid IN {931,1023 ,2971,3060})。不幸的是,這似乎不適用於iOS。你怎麼看呢? – 2014-09-29 17:40:41

0

當你有太多的標識符,你不能在一個SQL命令直接使用(無論您使用的參數或沒有)。

你必須所有的ID寫入到一個臨時表中,然後使用該檢查該表中的單個預測:

DELETE FROM PointsTable WHERE pid NOT IN MyTempTable 
+0

這是一個非常系統的方法。但是,我一直在使用Core Data一段時間,並且從不需要編寫任何SQL。這甚至可能在iOS上? – 2014-09-29 21:32:42

+0

有些機制可以直接使用SQL(使用原始SQLite C API或FMDB或其他庫),但不能與CoreData一起使用。 – 2014-09-30 07:06:35