我有一个对象OrganizationalUnit,它通过parentId属性链接到父实体。OrganizationalUnit还有一个主键属性ouId(映射自 JSON“_id”字段)。
这是我的代码:
RKManagedObjectMapping *ouMapping = [RKManagedObjectMapping mappingForClass:[OrganizationalUnit class] inManagedObjectStore:manager.objectStore];
[ouMapping mapAttributes:@"name",@"type", nil];
[ouMapping mapKeyPath:@"_id" toAttribute:@"ouId"];
[ouMapping setPrimaryKeyAttribute:@"ouId"];
[ouMapping mapKeyPath:@"parent" toAttribute:@"parentId"];
[ouMapping hasOne:@"parent" withMapping:ouMapping];
[ouMapping connectRelationship:@"parent" withObjectForPrimaryKeyAttribute:@"parentId"];
其工作方式与 RestKit 中的示例相同:
RKManagedObjectMapping* taskMapping = [RKManagedObjectMapping mappingForClass:[Task class] inManagedObjectStore:objectStore];
taskMapping.primaryKeyAttribute = @"taskID";
[taskMapping mapKeyPath:@"id" toAttribute:@"taskID"];
[taskMapping mapKeyPath:@"name" toAttribute:@"name"];
[taskMapping mapKeyPath:@"assigned_user_id" toAttribute:@"assignedUserID"];
[objectManager.mappingProvider setMapping:taskMapping forKeyPath:@"task"];
// Hydrate the assignedUser association via primary key
[taskMapping hasOne:@"assignedUser" withMapping:userMapping];
[taskMapping connectRelationship:@"assignedUser" withObjectForPrimaryKeyAttribute:@"assignedUserID"];
它应该在本地创建基于 id 的对象之间的关系。
不幸的是,在运行时,当接收数据的实际映射发生时,我收到错误:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x6d9c6f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key _id.'
任何想法?