核心数据:保存前无法处理待处理的更改.100次尝试后,上下文仍然很脏

Jim*_* Xu 6 core-data ios

我一直试图从崩解药中找到这次崩溃的原因:

致命异常NSInternalInconsistencyException在保存之前无法处理挂起的更改.100次尝试后,上下文仍然很脏.通常,这种递归弄脏是由错误的验证方法,-willSave或通知处理程序引起的.

我在代码的两个地方使用-willSave,我怀疑罪魁祸首是这个,但我不确定原因:

- (void)willSave {
    if (self.isDeleted) {
        PPInventory *inventory = [self primitiveInventory];
        [self unMonitorInventory:inventory];
    } else {
        PPInventory *inventory = [self primitiveInventory];
        NSMutableSet *missingItemIdSet = [NSMutableSet setWithArray:[inventory itemIds]];
        NSMutableArray *expiredInvItems = [NSMutableArray array];
        if (inventory != nil) {
            for (PPInventoryItem *invItem in [self inventoryItems]) {
                NSString *itemId = invItem.itemID;
                [missingItemIdSet removeObject:itemId];
                PPItem *item = [inventory getItemForId:itemId];
                if (item) {
                    [invItem updateFromItem:item];
                } else {
                    [expiredInvItems addObject:invItem];
                }
            }

            for (PPInventoryItem* invItem in expiredInvItems) {
                [self.managedObjectContext deleteObject:invItem];
            }

            for (PPItem *item in [inventory getItemsForIds:[missingItemIdSet allObjects]]) {
                [[item coreDataClass] inventoryItemFromItem:item forUser:self];
            }
        }
        //        if ([self hasChanges]) {
        //            PPLog(@"User %@ has changed keys: %@", self, [[self changedValues] allKeys]);
        //        }
    }
    [super willSave];
}
Run Code Online (Sandbox Code Playgroud)