Objective-c + RestKit - 在下一步之前等待响应

时间:2013-03-06 16:12:33

标签: ios objective-c asynchronous restkit

您好,并提前感谢您的帮助。我一直在寻找,但无法找到答案。我只为iOS编程了一个星期。

到目前为止,Web服务的所有连接都在运行,我已经创建了一个类和方法来执行这些调用。我正在进行标准登录,用户输入登录信息,应用程序将这些值传递给Web服务,并返回一个bool值,具体取决于信息是否与数据库中的任何内容匹配。之后,应用程序获取返回值,然后移动到下一个屏幕或显示错误警报。或者至少那是我的目标。

问题在于,条件是在进行其余调用或解析响应之前执行的,而我找不到解决方案的好运。我已经阅读过有关异步和同步调用的内容,但实现它们并没有太多运气。

这是通话代码:

//Class that has the methods for calling the web services
__restObj  = [[restCalls alloc] init];
bool login = [__restObj restLogin:user passwd:pass];
if (login) {
   [self performSegueWithIdentifier:@"adminLogin" sender:self];
}
else{
   UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect group name or password." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
   [alert show];
}

在实际发生POST之前执行的条件有效,并且始终为false。

这是我的方法:

- (bool)restLogin:(NSString*)user passwd:(NSString*)pass{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];

groupInfo *gi = [[groupInfo alloc] init];

gi.gName = user;
gi.pass = pass;


RKObjectMapping *userInfoMapping = [RKObjectMapping requestMapping];
[userInfoMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userInfoMapping
                                                                               objectClass:[groupInfo class]
                                                                               rootKeyPath:nil];
[objectManager addRequestDescriptor:requestDescriptor];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[objectManager postObject:gi
                     path:@"adminLoginIos"
               parameters:nil
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                      NSArray* statuses = [mappingResult array];
                      NSLog(@"Loaded statuses: %@", statuses);
                      _result = [statuses objectAtIndex:0];
                  }
                  failure:^(RKObjectRequestOperation *operation, NSError *error) {
                      NSLog(@"Hit error: %@", error);
                  }
 ];

return _result;
}

在此先感谢您,请记住,我对此非常陌生,所以感谢您的帮助,如果我的代码不是最佳,请告诉我。

此致 ChmlGr

1 个答案:

答案 0 :(得分:2)

你需要传入一个块然后在成功回调中,阻止返回_result。

基于您的结构的示例如下:

-(void) restLogin:(NSString*)user passwd:(NSString*)pass block:(void (^)(id))block {
    // Load the object model via RestKit
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    groupInfo *gi = [[groupInfo alloc] init];

    gi.gName = user;
    gi.pass = pass;


    RKObjectMapping *userInfoMapping = [RKObjectMapping requestMapping];
    [userInfoMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userInfoMapping
                                                                                   objectClass:[groupInfo class]
                                                                                   rootKeyPath:nil];
    [objectManager addRequestDescriptor:requestDescriptor];
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

    [objectManager postObject:gi
                         path:@"adminLoginIos"
                   parameters:nil
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                          NSArray* statuses = [mappingResult array];
                          NSLog(@"Loaded statuses: %@", statuses);
                          _result = [statuses objectAtIndex:0];
                          block(_result);
                      }
                      failure:^(RKObjectRequestOperation *operation, NSError *error) {
                          NSLog(@"Hit error: %@", error);
                          block(nil);
                      }
     ];

}

您对该方法的调用将类似于:

[__restObj restLogin:user passwd:pass block:^(id obj) {
    // do something here to translate that object into a BOOL and check value    
}];

我不使用RestKit,因此我无法验证这正是您所需要的,但它应该让您走上正确的道路。也就是说,如果您想查看AFNetworking,我写了一个我不介意分享的NetworkClient包装器。