Restkit + Objective-c - 多次调用同一个 Web 服务 [英] Restkit + Objective-c - Multiple calls to same web service

查看:62
本文介绍了Restkit + Objective-c - 多次调用同一个 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 restkit 和 iOS 的新手.我正在使用 xcode 4.2 和 RestKit 0.2.我一直在开发一个 iPhone 应用程序,它与我创建的一些 REST Web 服务进行通信,现在我遇到了一个问题.我的一些 Web 服务需要多次调用.第一个请求运行没有问题,但是当我再次尝试拨打电话时,出现此错误:

I'm new to restkit and iOS. I'm using xcode 4.2 and RestKit 0.2. I've been developing an iPhone app that communicates with some REST web services that I've created and now I'm facing a problem. A few of my web services are meant to be call more than once. The first request runs with no problem but when I try to make the call again, I get this error:

'NSInternalInconsistencyException',原因:'无法为与现有请求描述符相同的对象类添加请求描述符.'

'NSInternalInconsistencyException', reason: 'Cannot add a request descriptor for the same object class as an existing request descriptor.'

我确实理解此错误的含义,但还没有找到实现可重复使用的调用的方法.我有一种感觉,我做错了.下面是我用来调用 Web 服务的函数之一:

I do understand what this error means but have not found a way to implement the calls that can be reused. I have a feeling I'm doing it wrong. Below is one of the functions that I'm using to call the web service:

- (void) restAddPlayer:(addPlayer*)add block:(void (^)(id))block{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKObjectMapping *categoryRequestMapping = [RKObjectMapping requestMapping];
[categoryRequestMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}];

RKObjectMapping *reqMapping = [RKObjectMapping requestMapping];
[reqMapping addAttributeMappingsFromDictionary:@{@"plName": @"playerName"}];
[reqMapping addRelationshipMappingWithSourceKeyPath:@"groupInfo" mapping:categoryRequestMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:reqMapping objectClass:[addPlayer class] rootKeyPath:nil];

[objectManager addRequestDescriptor:requestDescriptor];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[objectManager postObject:add path:@"addPlayer"parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                      NSArray* statuses = [mappingResult array];
                     //NSLog(@"Loaded statuses: %@", statuses);
                     // _result = [statuses objectAtIndex:0];
                      block(statuses);

                  }
                 failure:^(RKObjectRequestOperation *operation, NSError *error){
                     NSLog(@"Hit error: %@", error);
                     block(nil);
                 }
    ];
}

我知道我正在添加一个请求描述符,当我再次调用该函数时,它试图添加另一个,从而导致错误.我想知道的是如何处理这个问题,以便我可以使用所发布对象中的不同信息多次调用 Web 服务.

I know I'm adding a request descriptor and when I call the function again it's trying to add another and thus the error. What I would like to know is how to handle this so I can call the web service more than once with different information in the object being post.

提前致谢.

问候

推荐答案

此例程中的所有内容,除了postObject"调用外,都应该在其他地方,例如应用启动时的应用委托.

Everything in this routine, except for the "postObject" call should probably be somewhere else, like in your app-delegate when the app starts up.

您正在设置映射、关系和请求描述符.您无需为每次调用都指定它们.只需设置一次,然后当您准备好时,只需调用postObject".您的 serializationType 可能也是如此——除非某些 REST 端点返回 XML 而其他端点返回 JSON,否则您可能可以设置一次,然后忘记它.

You're setting up mappings, relationships, a request descriptor. You don't need to keep specifying them for each call. Just set them up once, and then when you're ready, just call "postObject". The same probably goes for your serializationType as well -- unless some REST endpoints return XML and others return JSON, you're probably OK to set this once, and forget about it.

这篇关于Restkit + Objective-c - 多次调用同一个 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆