我一直在试图从崩解药中找到这次崩溃的原因:
致命异常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];
}