我在保存实体(DictionaryMarketSubcategoryEntity)后出现此错误,而不是再次保存它们。
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DictionaryMarketSubcategoryEntity 0x60000269e2b0> setValue:forUndefinedKey:]: the entity DictionaryMarketSubcategoryEntity is not key value coding-compliant for the key "(null)".'
*** First throw call stack:
(
0 CoreFoundation 0x00007ff8004278cb __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80004dba3 objc_exception_throw + 48
2 CoreFoundation 0x00007ff80042753d -[NSException init] + 0
3 CoreData 0x00007ff8049b8a02 -[NSMergePolicy _mergeToManyUnionRelationshipsForObject:andObject:] + 945
4 CoreData 0x00007ff8049bcf29 -[NSMergePolicy resolveConstraintConflicts:error:] + 5412
5 CoreData 0x00007ff8049bb99a -[NSMergePolicy resolveConflicts:error:] + 210
6 CoreData 0x00007ff80497a534 -[NSManagedObjectContext save:] + 3215
7 FCDev 0x0000000101624e04 $s5FCDev13CoreDataStackC20saveContextIfChangedyyF + 1524
8 FCDev 0x0000000101573f8b $s5FCDev36CoreDataStorageServiceImplementationC5writeyyF + 43
9 FCDev 0x0000000101575710 $s5FCDev36CoreDataStorageServiceImplementationCAA0dE0A2aDP5writeyyFTW + 16
10 FCDev 0x0000000100a02291
这是我保存的实体(DictionaryMarketSubcategoryEntity)。它有子类别(DictionaryMarketSubcategoryEntity)。如果我注解并删除子类别,一切正常。似乎当我保存DictionaryMarketCategoryEntity数组时,它保存了子实体(DictionaryMarketSubcategoryEntity),重新保存后,合并策略崩溃。为什么?
下面是初始化代码:
extension DictionaryMarketCategoryEntity: JSONInitializable {
convenience init(json: JSON) {
self.init(context: AppInstances.shared.storage.context)
self.backgroundColor = json["backgroundColor"].stringValue
self.backgroundImagePath = json["backgroundImage"].stringValue
self.descriptionText = json["description"].stringValue
self.icon = json["icon"].stringValue
self.id = json["id"].stringValue
self.imagePath = json["image"].stringValue
self.parent = json["parent"].stringValue
self.span = json["span"].int16Value
self.title = json["title"].stringValue
self.type = json["type"].stringValue
let childrenJson = json["children"].arrayValue
let childrenEntities = childrenJson.map { DictionaryMarketSubcategoryEntity(json: $0) }
let childrenSet = NSSet(array: childrenEntities)
addToChildren(childrenSet)
}
}
extension DictionaryMarketSubcategoryEntity {
convenience init(json: JSON) {
self.init(context: AppInstances.shared.storage.context)
self.id = json["id"].stringValue
self.title = json["title"].stringValue
self.descriptionText = json["description"].stringValue
self.imagePath = json["image"].stringValue
self.backgroundImagePath = json["backgroundImage"].stringValue
self.backgroundColor = json["backgroundColor"].stringValue
self.icon = json["icon"].stringValue
self.parent = json["parent"].stringValue
self.span = json["span"].int16Value
self.type = json["type"].stringValue
}
}
1条答案
按热度按时间c86crjj01#
我已经在
subcategory
中添加了父关系。然后在主实体中使用addToChildrenEntities
-由xcode函数自动生成。之后错误消失。这不是因为JSON。似乎如果子实体(子类别)不知道什么是他的父实体,核心数据无法合并。