在 iOS 的 RestKit 中执行 postObject 时忽略响应

发布于 2024-12-12 02:42:42 字数 198 浏览 0 评论 0原文

我正在使用 RestKit 连接到我们的 WCF 数据服务。

由于响应映射问题,我在使用 RKObjectManager 的 postObject 函数添加实体时遇到问题。

添加实体时,WCF 数据服务返回 201 状态代码和新添加的实体(作为响应)。

是否可以忽略响应并仅使用返回的状态代码来检查添加是否成功?

波努

I am using RestKit for connecting to our WCF Data Services.

I am having issues with adding an entity using RKObjectManager's postObject function due to response mapping issues.

When an entity is added, WCF Data Services returns a 201 status code and the newly added entity(as response).

Is it possible to ignore the response and just use the status code returned to check if the add succeeded?

Ponnu

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

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

发布评论

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

评论(3

俯瞰星空 2024-12-19 02:42:43

为什么要忽略从服务器返回的新添加的实体?映射该结果对于保持本地表示与服务器表示的同步很有用。服务器可能已经覆盖了对象的某些字段(例如对象 ID),并且您想要跟踪它。

如果出现映射错误,可能是因为对 POST 操作的响应返回对象的表示形式,该表示形式与 GET 返回的对象表示形式不同。
您是否尝试过使用:

- (RKObjectLoader*)postObject:(id)object mapResponseWith:(RKObjectMapping*)objectMapping delegate:(id)delegate

代替并指定更合适的映射返回的数据?

Why would you want to ignore the newly added entity returned from the server? Mapping that result is useful to keep in sync your local representation with the server's one. The server may have overwritten some field of your object like the object id and you want to keep track of it.

If you have mapping error it's probably because the response to the POST action returns a representation of your object which differs from the one returned with a GET.
Have you tried using:

- (RKObjectLoader*)postObject:(id<NSObject>)object mapResponseWith:(RKObjectMapping*)objectMapping delegate:(id<RKObjectLoaderDelegate>)delegate

instead and specify a more suitable mapping for the data returned?

浮萍、无处依 2024-12-19 02:42:43

这里的问题可能是更改 REST 服务,因此一个简单的解决方案是在 postObject 调用某个资源路径的情况下忽略对 didFailWithError 的回调。

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
if ([objectLoader wasSentToResourcePath:@"/rest/api/returns/201" method:RKRequestMethodPOST] && [[objectLoader response] statusCode]==201) {
    NSLog(@"Object created");
} else {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Communication error"
                                                        message:[NSString stringWithFormat:@"Received status code %d: %@",                                                                                               objectLoader.response.statusCode,                                                                                               error.localizedDescription]                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

The problem here can be to alter the REST service, so instead a simple solution would be to ignore the callback to didFailWithError in case of postObject calls to a certain resource path.

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
if ([objectLoader wasSentToResourcePath:@"/rest/api/returns/201" method:RKRequestMethodPOST] && [[objectLoader response] statusCode]==201) {
    NSLog(@"Object created");
} else {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Communication error"
                                                        message:[NSString stringWithFormat:@"Received status code %d: %@",                                                                                               objectLoader.response.statusCode,                                                                                               error.localizedDescription]                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}
全部不再 2024-12-19 02:42:43

创建一个简单的 RKObjectMapping,它不关心响应中的任何参数。

[RKObjectMapping mappingForClass: [NSNull class]];

Create a trivial RKObjectMapping that does not care about any parameters in response.

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