核心数据:表达式树太大

时间:2014-09-29 17:24:39

标签: ios objective-c sqlite core-data

我需要一些帮助这个方法:

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

我已经搜索过,当谓词太长,如WHERE id=1 OR id=2 OR id=3 ...时,会发生这种情况,但我不知道如何解决这个问题。有人有想法吗?

提前致谢, 皮特。

2 个答案:

答案 0 :(得分:1)

您的谓词应简化为以下内容

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

答案 1 :(得分:0)

如果标识符太多,则无法在SQL命令中直接使用它们(无论您是否使用参数)。

您必须将所有ID写入临时表,然后使用检查该表的单个谓词:

DELETE FROM PointsTable WHERE pid NOT IN MyTempTable