我遇到了核心数据错误,当我尝试保存Group <<->> Contact之间的关系时。
我通过hashUser从coredata中获取一个Contact,通过uid获取一个Group,并尝试使用func添加to-many关系。
// extension CXDMContact
func addGroup(value: CXDMGroup) {
let items = self.mutableSetValueForKey("groups");
items.addObject(value)
}
当我尝试保存时,出现异常。
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. [<CXDMGroup 0x7fb7924aa770> valueForUndefinedKey:]: the entity Group is not key value coding-compliant for the key "id". with userInfo {
NSTargetObjectUserInfoKey = "<CXDMGroup: 0x7fb7924aa770> (entity: Group; id: 0xd0000000000c0000 <x-coredata://E66299CB-12A5-4152-8B49-8E9B3B68762F/Group/p3> ; data: {\n contacts = (\n \"0xd000000000040002 <x-coredata://E66299CB-12A5-4152-8B49-8E9B3B68762F/Contact/p1>\"\n );\n uid = 5;\n})";
NSUnknownUserInfoKey = id;
}
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CXDMGroup 0x7fb7924aa770> valueForUndefinedKey:]: the entity Group is not key value coding-compliant for the key "id".'
但是我没有任何ID字段,如果我此时查看managedObjectContext - 我会看到
(lldb) po managedObjectContext.updatedObjects
▿ 2 elements
- [0] : <CXDMContact: 0x7fb7927f5270> (entity: Contact; id: 0xd000000000040002 <x-coredata://E66299CB-12A5-4152-8B49-8E9B3B68762F/Contact/p1> ; data: {
chatStatus = nil;
companyId = nil;
companyName = OWN;
companySubscription = nil;
connectedLoads = nil;
countryCode2 = nil;
departmentName = nil;
email = nil;
firstname = Ownname;
groups = (
"0xd0000000000c0000 <x-coredata://E66299CB-12A5-4152-8B49-8E9B3B68762F/Group/p3>"
);
jobTitle = nil;
lastname = Lastname;
messages = "<relationship fault: 0x7fb7924a25b0 'messages'>";
phone1 = nil;
phone2 = nil;
serialNumber = nil;
status = Offline;
timeZoneName = nil;
userHash = 1002;
})
- [1] : <CXDMGroup: 0x7fb7924aa770> (entity: Group; id: 0xd0000000000c0000 <x-coredata://E66299CB-12A5-4152-8B49-8E9B3B68762F/Group/p3> ; data: {
contacts = (
"0xd000000000040002 <x-coredata://E66299CB-12A5-4152-8B49-8E9B3B68762F/Contact/p1>"
);
uid = 5;
})
(lldb) po managedObjectContext.insertedObjects
0 elements
(lldb) po managedObjectContext.deletedObjects
0 elements
我不明白 "id" 键在哪里? 请帮忙。