使用RestKit进行同步HTTP调用 [英] Make a synchronous HTTP call with RestKit
问题描述
将用户登录到我的应用程序时,我需要仅使用用户名从服务器中删除用户对象。这将返回我需要的userId(以及其他内容)以进行其他API调用。从那时起,我将使用userId进行一些其他HTTP调用。如何在发送其他调用之前进行同步调用以完全拉下用户对象?
When logging a user into my application I need to pull a user object down from the server using only the username. This returns the userId (among other things) that I need in order to make other API calls. From that point I'll make a couple other HTTP calls using the userId. How can I make a synchronous call to completely pull down the user object before sending the other calls?
我在我的app delegate类中设置了我的对象映射,完美,并使用此代码从服务器拉下用户对象:
I've setup my object mapping in my app delegate class, which works perfectly, and am using this code to pull the user object down from the server:
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/users/" stringByAppendingString:[_userNameField text]] delegate:self];
这是我尝试过的......如下所示:使用RestKit进行同步通话
This is what I've tried... as suggested here: Making synchronous calls with RestKit
RKObjectLoader* loader = [[RKObjectManager sharedManager] objectLoaderForObject:currentUser method:RKRequestMethodPUT delegate:nil];
RKResponse* response = [loader sendSynchronously];
但是这段代码(1)使用了弃用的方法objectLoaderForObject和(2)崩溃说'无法为HTTP方法'POST''
找到类型为'(null)'的对象的可路由路径。
However this code (1) uses the deprecated method objectLoaderForObject and (2) crashes saying 'Unable to find a routable path for object of type '(null)' for HTTP Method 'POST''
.
推荐答案
撇开这是否是iPhone应用程序的理想设计的问题,我能够完成我所希望的使用块。
Putting aside the question of whether this is the ideal design for an iPhone application, I was able to accomplish what I was hoping using blocks.
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/users/" stringByAppendingString:[_userNameField text]] usingBlock:^(RKObjectLoader* loader) {
loader.onDidLoadResponse = ^(RKResponse *response) {
NSLog(@"Response: \n%@", [response bodyAsString]);
};
loader.onDidLoadObjects = ^(NSArray *objects) {
APIUser *apiUser = [objects objectAtIndex:0];
NSLog(@"user_id is %i", apiUser.user_id);
};
loader.onDidFailWithError = ^(NSError *error) {
UIAlertView *badLoginAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"LOGIN_FAILED", nil)
message:NSLocalizedString(@"BAD PASSWORD OR USERNAME", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil];
[badLoginAlert show];
};
}];
希望这有助于某人。
这篇关于使用RestKit进行同步HTTP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!