我使用RestKit
启用了ARC的项目,虽然我的大多数请求是异步完成的,但我遇到执行同步请求的问题:
在我的AppDelegate
:
else if (![IKUserController loggedInUserIsAuthenticated]) {
IKLoginViewController *loginVC = [[IKLoginViewController alloc] init];
loginVC.scenario = SCENARIO_EXISTING;
[self.window.rootViewController presentModalViewController:loginVC animated:YES];
}
在loggedInUserIsAuthenticated
的实施中:
+ (BOOL)loggedInUserIsAuthenticated {
IKUser *user = [IKUserController loggedInUser];
if (!user) {
return NO;
}
else {
NSString *username = user.userName;
NSString *password = user.userPassword;
if ([IKUserController loginWithUsername:username password:password]) {
return YES;
}
else {
return NO;
}
}
return NO;
}
和loginWithUserName:password:
+ (BOOL)loginWithUsername:(NSString *)username password:(NSString *)password {
//return YES;
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:username, @"username", password, @"password", nil];
RKResponse *response = [[[RKClient sharedClient] post:@"/user/authenticate" params:params delegate:nil] sendSynchronously];
if (response.isOK) {
return YES;
}
else {
return NO;
}
return NO;
}
和错误:
*** Assertion failure in -[RKRequestQueue removeRequest:decrementCounter:], /Users/admin/Documents/dev/RestKit/Code/Network/RKRequestQueue.m:350
答案 0 :(得分:2)
RKClient方法 post 已经将请求添加到默认请求队列,所以我认为问题是您发送请求两次 - 一次异步和一次同步。而不是使用RKClient post 方法,手动配置请求。在RKClient上有一个方法 setupRequest ,这将使你更容易,你只需要定义url,方法和参数,如下所示:
RKRequest* req = [RKRequest requestWithURL:reqURL delegate:self];
[req setMethod:RKRequestMethodPOST];
[req setParams:params];
[client setupRequest:req];
[req sendSynchronously];