7

我一直试图从 crashlytics 中找到导致崩溃的原因:

致命异常 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];
}
4

0 回答 0