我正在尝试通过 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
,一个单独的NSArray
被调用memberIDs
保存所有成员用户的字符串 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"];
它抱怨,显然是因为关键路径是空的
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) {}];
以防万一这对处于类似情况的其他人有所帮助。我采用的方法可能仍然存在一些问题,但到目前为止一切都很好(它甚至可以处理乱序加载,这很好) - 如果我的原始问题有答案,我仍然很乐意听取任何人的意见。