我的服务器收到了 JSON 响应。从 RestKit 日志中获取的数据如下所示:
sourceObject: (
{
place = {
"place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034";
placeinfo = {
latitude = "12.5846738815";
longtitude = "55.6815948486";
"place_id" = "3cc1e372-f9d9-11e0-aba9-9415ab1a8034";
};
timestamp = "2011-10-19 00:33:44";
};
},
{
place = {
"place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034";
placeinfo = {
latitude = "12.5720376968";
longtitude = "55.6785774231";
"place_id" = "b65e36e0-f9d9-11e0-aba9-9415ab1a8034";
};
timestamp = "2011-10-19 00:37:08";
};
},
{
Timestamps = {
"latest_update" = "2011-10-18 17:12:09";
uuid = "8c6fb842-f99b-11e0-aba9-9415ab1a8034";
};
} )and targetObject: (null)
数据映射到 3 个对象:
Place、Placeinfo 和 LatestDBUpdate。
问题是:
在上面的 JSON 响应中有 2 个地点对象和 2 个嵌套的 PlaceInfo 对象,以一对多关系连接。
还有一个与 Timestamps 关键字相关的 LatestDBUpdate 对象。
Restkit 将映射 3 个 Place 对象,其中一个将全部为 NULL。其他 2 个 Place 对象已正确映射,并且关系也正确。它当然应该只映射 2 个 Place 对象!
LatestDBUpdate 映射也正确映射时间戳。
我已经确定,如果我删除 JSON 响应的时间戳部分,那么映射是正确的,只有 2 个位置对象。
但我需要时间戳部分在那里!我不知道如何解决这个问题!任何想法 - 我真的可以使用一些输入,因为我已经使用了几个小时!
以下是使用的映射设置:
//Place mapping
if (!self.placeManagedObject){
self.placeManagedObject = [RKManagedObjectMapping mappingForClass:[Place class]];
self.placeManagedObject.primaryKeyAttribute = @"UUID";
self.placeManagedObject.setDefaultValueForMissingAttributes = YES;
[self.placeManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"];
[self.placeManagedObject mapKeyPath:@"timestamp" toAttribute:@"timestamp"];
}
//PlaceInformation mapping
if (!self.placeInfoManagedObject){
self.placeInfoManagedObject = [RKManagedObjectMapping mappingForClass:[PlaceInfo class]];
self.placeInfoManagedObject.primaryKeyAttribute = @"UUID";self.placeInfoManagedObject.setDefaultValueForMissingAttributes = YES;
[placeInfoManagedObject mapKeyPath:@"place_id" toAttribute:@"UUID"];
[placeInfoManagedObject mapKeyPath:@"longtitude" toAttribute:@"longtitude"];
[placeInfoManagedObject mapKeyPath:@"latitude" toAttribute:@"latitude"];
}
//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]];
self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update";
[self.latestDBUpdateManagedObject mapKeyPath:@"latest_update"toAttribute:@"latest_update"];
[self.latestDBUpdateManagedObject mapKeyPath:@"uuid" toAttribute:@"uuid"];
}
//Set mapping relations
[self.placeManagedObject mapRelationship:@"placeinfo" withMapping:self.placeInfoManagedObject];
// Register mapping with the provider -
[self.objectManager.mappingProvider setMapping:self.placeManagedObject forKeyPath:@"place"];
[self.objectManager.mappingProvider setMapping:self.placeInfoManagedObject forKeyPath:@"placeinfo"];
[self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"];