RestKit -“高级数据对象”的对象映射存在问题?

发布于 2024-12-10 01:18:36 字数 2175 浏览 0 评论 0原文

我的映射有问题: latest_update 字段 JSON 数据。

从我的 web 服务接收此 JSON 数据:

{"Places":[ 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"latest_update":"2011-10-13 12:16:17"}] 

}

以及我用于托管映射的代码片段:

 //Place mapping 
    if (!self.placeManagedObject){ 
        self.placeManagedObject = [RKManagedObjectMapping 
mappingForClass:[Place class]]; 
        self.placeManagedObject.primaryKeyAttribute = @"UUID"; 
        self.placeManagedObject.rootKeyPath = @"places"; 
        [self.placeManagedObject mapKeyPath:@"place_ID" 
toAttribute:@"UUID"]; 
        [self.placeManagedObject mapKeyPath:@"timestamp" 
toAttribute:@"timestamp"]; 
        [self.placeManagedObject mapRelationship:@"PlaceInformation" 
withMapping:self.placeInfoManagedObject]; 
        // Register mapping with the provider - means it will look for 
places in the JSON input 
        [objectManager.mappingProvider 
setMapping:self.placeManagedObject forKeyPath:@"places"]; 
    } 
    //latestDBUpdate timestamp mapping 
    if (!self.latestDBUpdateManagedObject){ 
        self.latestDBUpdateManagedObject = [RKManagedObjectMapping 
mappingForClass:[LatestDBUpdate class]]; 
        self.latestDBUpdateManagedObject.primaryKeyAttribute = 
@"latest_update"; 
        self.latestDBUpdateManagedObject.rootKeyPath = @"places"; 
        [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" 
toAttribute:@"latest_update"]; 
        // Register mapping with the provider - means it will look for 
places in the JSON input 
        [objectManager.mappingProvider 
setMapping:self.latestDBUpdateManagedObject 
forKeyPath:@"latest_update"]; 
    } 

RestKit 将正确映射 Place 对象,即: {“place_ID”:“7cceedda-ed3a-11e0-a1a8-858e3974979a”, “时间戳”:“2011-10-02 23:24:42” } ... 放置物体, 但 latest_update 未映射到 LatestDBUpdate 类 对象,我无法以任何方式让它工作。

我希望有人知道它是如何完成的,因为几个小时 搜索和尝试并没有让我更接近解决方案。 感谢托马斯

Im having a problem with the mapping of the: latest_update field in
the JSON data.

Recieving this JSON data from my webservice:

{"Places":[ 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x", 
     "timestamp": "2011-10-02 23:24:42" 
    }, 
    {"latest_update":"2011-10-13 12:16:17"}] 

}

And a snip of the code i use for the managed mapping:

 //Place mapping 
    if (!self.placeManagedObject){ 
        self.placeManagedObject = [RKManagedObjectMapping 
mappingForClass:[Place class]]; 
        self.placeManagedObject.primaryKeyAttribute = @"UUID"; 
        self.placeManagedObject.rootKeyPath = @"places"; 
        [self.placeManagedObject mapKeyPath:@"place_ID" 
toAttribute:@"UUID"]; 
        [self.placeManagedObject mapKeyPath:@"timestamp" 
toAttribute:@"timestamp"]; 
        [self.placeManagedObject mapRelationship:@"PlaceInformation" 
withMapping:self.placeInfoManagedObject]; 
        // Register mapping with the provider - means it will look for 
places in the JSON input 
        [objectManager.mappingProvider 
setMapping:self.placeManagedObject forKeyPath:@"places"]; 
    } 
    //latestDBUpdate timestamp mapping 
    if (!self.latestDBUpdateManagedObject){ 
        self.latestDBUpdateManagedObject = [RKManagedObjectMapping 
mappingForClass:[LatestDBUpdate class]]; 
        self.latestDBUpdateManagedObject.primaryKeyAttribute = 
@"latest_update"; 
        self.latestDBUpdateManagedObject.rootKeyPath = @"places"; 
        [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" 
toAttribute:@"latest_update"]; 
        // Register mapping with the provider - means it will look for 
places in the JSON input 
        [objectManager.mappingProvider 
setMapping:self.latestDBUpdateManagedObject 
forKeyPath:@"latest_update"]; 
    } 

RestKit will map the Place objects correct i.e:
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a",
"timestamp": "2011-10-02 23:24:42"
}
...
into place objects,
but the latest_update is not mapped into the
LatestDBUpdate class object, and i cannot in any way get it to work.

I hope the someone has the answer to how it is done, because hours of
searching and trying has brought me no closer to a solution.
Thanx Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2024-12-17 01:18:36

所以我终于想通了。
基本上是一些小调整,以及一些代表我的错误:-/

因此,我将 Web 服务的 JSON 输出更改为以下格式:

{"Places":[ 
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", 
 "timestamp": "2011-10-02 23:24:42" 
}, 
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x", 
 "timestamp": "2011-10-02 23:24:42" 
}], 
"Timestamps":[
{"latest_update":"2011-10-13 12:16:17"}]

我在映射 og 时间戳中出现错误 -> latest_update,所以我解决了这个问题:

//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
    self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]];
    self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update";
    self.latestDBUpdateManagedObject.rootKeyPath = @"Timestamps";
    [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" toAttribute:@"latest_update"];

    // Register mapping with the provider - means it will look for the keyword Timestamps in the JSON input
    [self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"];

然后我的 get 方法出现了一个重大错误。基本上,我告诉 RestKit,响应应该只由我的 Place 映射进行映射,因此不会检查 JSON 输入中的所有其他数据是否有其他关键字。
为了显示差异,我展示了我使用的两种 GET 方法。第一个将仅使用指定的映射方案进行映射,第二个将检查所有注册的映射方案:

GET 方法编号 1:(这在上述上下文中不起作用!)

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl objectMapping:self.placeManagedObject delegate:self];

GET 方法编号 2:(此处检查所有映射方案并且两个 Place 对象以及我的 LastDBUpdate 对象都已映射!)

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl delegate:self];

希望有一天有人可能需要这个:-D

Thomas

So i finally figured it out.
Basically some small tweaks, and a couple of errors on my behalf:-/

So i changed the JSON output from my webservice to this formatting:

{"Places":[ 
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974979a", 
 "timestamp": "2011-10-02 23:24:42" 
}, 
{"place_ID": "7cceedda-ed3a-11e0-a1a8-858e3974933x", 
 "timestamp": "2011-10-02 23:24:42" 
}], 
"Timestamps":[
{"latest_update":"2011-10-13 12:16:17"}]

I had a an error in the mapping og Timestamps -> latest_update, so i fixxed that:

//latestDBUpdate timestamp mapping
if (!self.latestDBUpdateManagedObject){
    self.latestDBUpdateManagedObject = [RKManagedObjectMapping mappingForClass:[LatestDBUpdate class]];
    self.latestDBUpdateManagedObject.primaryKeyAttribute = @"latest_update";
    self.latestDBUpdateManagedObject.rootKeyPath = @"Timestamps";
    [self.latestDBUpdateManagedObject mapKeyPath:@"latest_update" toAttribute:@"latest_update"];

    // Register mapping with the provider - means it will look for the keyword Timestamps in the JSON input
    [self.objectManager.mappingProvider setMapping:self.latestDBUpdateManagedObject forKeyPath:@"Timestamps"];

And then i had a major error in my get method. Basically i told RestKit that the response should only be mapped by my Place mapping and thus not check all other data in the JSON input for other keywords.
Just to show the difference i present both GET methods i used. The first will ONLY map with the specified mapping scheme, and number two will check with all registred mapping schemes:

GET method number 1: (That does not work in the above context!)

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl objectMapping:self.placeManagedObject delegate:self];

GET method numer 2: (Here all mapping schemes are checked and both Place objects are mapped, as well as my LatestDBUpdate object!)

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:gPlaceListBaseUrl delegate:self];

Hopefully someone might need this someday:-D

Thomas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文