我有一个用于此类项目的 JSON 数据结构
{
"id": "8a1a39479d03d959",
"sortOrder": 3,
"timestamp": 1381669499.11,
}
和另一个只有一个项目的文件结构:
{
"id": "d88e3c3d1630db4c",
"item": "8a1a39479d03d959",
"timestamp": 1381669505.364,
"version": 2
}
我一直在努力让这种映射起作用,以至于我向[files]
JSON 项目添加了一个列表,只是为了映射关系。但是,阅读Google group 上的一篇较早的帖子,我发现它应该可以像这样映射这些关系(未经测试的代码,只是解释上面的链接):
RKManagedObjectMapping *itemMapping = [RKManagedObjectMapping mappingForClass:[Item class]];
itemMapping.primaryKeyAttribute = @"itemID";
[itemMapping mapKeyPath:@"sortOrder" toAttribute:@"sortOrder"];
[itemMapping mapKeyPath:@"id" toAttribute:@"itemID"];
RKManagedObjectMapping *fileMapping = [RKManagedObjectMapping mappingForClass:[File class]];
fileMapping.primaryKeyAttribute = @"fileID";
[fileMapping mapKeyPath:@"id" toAttribute:@"fileID"];
[fileMapping mapKeyPath:@"item" toAttribute:@"itemID"];
[fileMapping mapKeyPath:@"version" toAttribute:@"version"];
[fileMapping hasOne:@"item" withMapping:itemMapping];
[fileMapping connectRelationship:@"organization" withObjectForPrimaryKeyAttribute:@"itemID"];
但是hasOne:withMapping:
在 RestKit 0.20 中不再存在。如今,将一对多关系映射到 JSON 中作为简单字符串返回的 id 的正确方法是什么?仅供参考,我在这个项目中使用 CoreData 集成。