通过 ID 数组映射 RestKit 中的关系不起作用
Posted
技术标签:
【中文标题】通过 ID 数组映射 RestKit 中的关系不起作用【英文标题】:Mapping relationships in RestKit through an array of IDs doesn't work 【发布时间】:2012-10-24 17:03:51 【问题描述】:我正在尝试通过 RestKit 将用户和组映射到 CoreData 对象,保持两者之间的关系。
用户的 JSON 类似于
"users":
["fullname": "John Doe", "_id": "1"
...
]
组的 JSON 类似于
"groups":
["name": "John's group", "_id": "a",
"members": ["1", "2"]
...
]
我也有对应的 Core Data 对象,User
和 Group
,在组和用户之间具有一对多的关系映射。 Group
中的关系属性称为members
,另外一个称为memberIDs
的NSArray
包含所有成员用户的字符串ID 数组。
我想在 RestKit 中完成的是加载这些对象并为我映射关系。加载用户的代码是直接的标准代码,加载组的代码类似于
RKObjectManager* objectManager = [RKObjectManager sharedManager];
RKManagedObjectMapping* groupMapping = [RKManagedObjectMapping mappingForClass:[Group class] inManagedObjectStore:objectManager.objectStore];
groupMapping.primaryKeyAttribute = @"identifier";
[groupMapping mapKeyPath:@"_id" toAttribute:@"identifier"];
[groupMapping mapKeyPath:@"name" toAttribute:@"name"];
[groupMapping mapKeyPath:@"members" toAttribute:@"memberIDs"];
[objectManager.mappingProvider setMapping:groupMapping forKeyPath:@"groups"];
// Create an empty user mapping to handle the relationship mapping
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:objectManager.objectStore];
[groupMapping hasMany:@"members" withMapping:userMapping];
[groupMapping connectRelationship:@"members" withObjectForPrimaryKeyAttribute:@"memberIDs"];
RKObjectRouter* router = objectManager.router;
[router routeClass:[Group class] toResourcePath:@"/rest/groups/:identifier"];
[router routeClass:[Group class] toResourcePath:@"/rest/groups/" forMethod:RKRequestMethodPOST];
// Assume url is properly defined to point to the right path...
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:url usingBlock:^(RKObjectLoader *loader) ];
当我运行此代码时,我多次收到以下警告(似乎每个关系大约两次):
2012-10-24 08:55:10.170 Test[35023:18503] W restkit.core_data.cache:RKEntityByAttributeCache.m:205 Unable to add object with nil value for attribute 'identifier': <User: 0x86f7fc0> (entity: User; id: 0x8652400 <x-coredata:///User/t01A9EDBD-A523-4806-AFC2-9B06873D764E531> ; data:
fullname = nil;
identifier = nil;
)
奇怪的是关系设置正确(甚至查看了 sqlite 数据库,那里的一切看起来都很好)但我对 RestKit 代码中明显出错的地方不满意,而且它似乎有一些东西与我在上面的代码中创建的虚假用户映射有关(它是空的,因为在 ID 数组中没有要映射的内容)。
我尝试了替代方法,例如当我添加键路径映射时:
[userMapping mapKeyPath:@"" toAttribute:@"identifier"];
报错,明显是因为key path是空的
2012-10-24 09:24:11.735 Test[35399:14003] !! Uncaught exception !!
[<__NSCFString 0xa57f460> valueForUndefinedKey:]: this class is not key value coding-compliant for the key .
如果我尝试使用真正的User
映射
[groupMapping hasMany:@"members" withMapping:[[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:@"users"]];
我收到以下错误
2012-10-24 09:52:49.973 Test[35799:18403] !! Uncaught exception !!
[<__NSCFString 0xa56e9a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key _id.
2012-10-24 09:52:49.973 Test[35799:18403] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0xa56e9a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key _id.'
似乎 RestKit 正在尝试将 memberIDs 数组中的 ID 字符串本身映射到 User
对象,这没有任何意义。我见过一些例子,其中关系数组是一个带有键的字典列表(例如,称为id
),带有另一个对象的引用 ID,例如:
"groups":
["name": "John's group", "_id": "a",
"members": [
"_id": "1",
"_id": "2"
]
...
]
但我不想以这种方式更改我的 JSON(此外,我希望 RestKit 足够灵活以支持另一种方式。)
有谁知道在 RestKit 中进行这种关系映射的正确方法是什么?
更新
我最终修改了 REST 接口以发送包含用户对象 ID 的字典列表(就像上面的最后一个示例一样),并让它工作。仍然对设置不完全满意,但代码现在看起来像
RKObjectManager* objectManager = [RKObjectManager sharedManager];
RKManagedObjectMapping* groupMapping = [RKManagedObjectMapping mappingForClass:[Group class] inManagedObjectStore:objectManager.objectStore];
groupMapping.primaryKeyAttribute = @"identifier";
[groupMapping mapKeyPath:@"_id" toAttribute:@"identifier"];
[groupMapping mapKeyPath:@"name" toAttribute:@"name"];
[groupMapping mapKeyPath:@"members._id" toAttribute:@"memberIDs"];
[objectManager.mappingProvider setMapping:groupMapping forKeyPath:@"groups"];
// Create an empty user mapping to handle the relationship mapping
RKManagedObjectMapping* userMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:objectManager.objectStore];
userMapping.primaryKeyAttribute = @"identifier";
[userMapping mapKeyPath:@"_id" toAttribute:@"identifier"];
[groupMapping hasMany:@"members" withMapping:userMapping];
[groupMapping connectRelationship:@"members" withObjectForPrimaryKeyAttribute:@"memberIDs"];
RKObjectRouter* router = objectManager.router;
[router routeClass:[Group class] toResourcePath:@"/rest/groups/:identifier"];
[router routeClass:[Group class] toResourcePath:@"/rest/groups/" forMethod:RKRequestMethodPOST];
// Assume url is properly defined to point to the right path...
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:url usingBlock:^(RKObjectLoader *loader) ];
以防万一这对处于类似情况的其他人有所帮助。我采用的方法可能仍然存在一些问题,但到目前为止一切都很好(它甚至可以处理乱序加载,这很好) - 如果我的原始问题有答案,我仍然很乐意听取任何人的意见。
【问题讨论】:
【参考方案1】:找到了一种方法来完成这项工作,并且还意识到我的问题中的更新方法只应该用于嵌套对象,而不是引用对象。这个帖子让我走上了正轨:https://groups.google.com/forum/#!msg/restkit/swB1Akv2lTE/mnP2OMSqElwJ(如果你正在处理 RestKit 中的关系,值得一读)
假设 JSON 组仍然看起来像原始 JSON:
"groups":
["name": "John's group", "_id": "a",
"members": ["1", "2"]
...
]
还假设用户被映射到一个键路径users
(即类似于[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
)映射关系的正确方法如下所示:
RKObjectManager* objectManager = [RKObjectManager sharedManager];
RKManagedObjectMapping* groupMapping = [RKManagedObjectMapping mappingForClass:[Group class] inManagedObjectStore:objectManager.objectStore];
groupMapping.primaryKeyAttribute = @"identifier";
[groupMapping mapKeyPath:@"_id" toAttribute:@"identifier"];
[groupMapping mapKeyPath:@"name" toAttribute:@"name"];
[groupMapping mapKeyPath:@"members" toAttribute:@"memberIDs"];
[objectManager.mappingProvider setMapping:groupMapping forKeyPath:@"groups"];
[groupMapping mapKeyPath:@"users" toRelationship:@"members" withMapping:[[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:@"users"] serialize:NO];
[groupMapping connectRelationship:@"members" withObjectForPrimaryKeyAttribute:@"memberIDs"];
RKObjectRouter* router = objectManager.router;
[router routeClass:[Group class] toResourcePath:@"/rest/groups/:identifier"];
[router routeClass:[Group class] toResourcePath:@"/rest/groups/" forMethod:RKRequestMethodPOST];
// Assume url is properly defined to point to the right path...
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:url usingBlock:^(RKObjectLoader *loader) ];
当我之前尝试过这种方法时,让我感到困惑的是这行
[groupMapping mapKeyPath:@"users" toRelationship:@"members" withMapping:[[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:@"users"] serialize:NO];
第一个键路径指的是被引用对象的键路径映射(在这种情况下为users
),而不是被映射的group
对象内关系的键路径(本来是@在这种情况下为 987654330@)。
有了这个变化,所有的关系都按预期工作,我很高兴。
【讨论】:
嘿!我正在尝试做同样的事情,但我想知道,CoreData 中的 memberIDS 类型是什么?我会尽快尝试的!谢谢! @abisson 检查来自 Blake Watters 的答案:groups.google.com/d/msg/restkit/9MG0CPi1aS0/olQGaSiJQjAJ以上是关于通过 ID 数组映射 RestKit 中的关系不起作用的主要内容,如果未能解决你的问题,请参考以下文章
RestKit 2.0 - 将 json 数组映射到实体关系会丢失数组序列