RestKit映射对象引用id

时间:2012-06-06 11:34:05

标签: ios5 mapping restkit

我有一个对象 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.'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为我已经找到问题的根源,可能是由于孩子们因为他们尚不存在而无法引用父母而引起的。