AFNetworking和同步请求,以完成在线注册过程

时间:2013-04-15 22:19:03

标签: ios objective-c cocoa-touch afnetworking

我正在使用AFNetworking而且我已经读过不鼓励使用同步响应。但是我需要检查用户是否已经存在于在线数据库中,然后才能进入应用程序的下一个阶段。是的,这是典型的注册过程。

我的代码就是它返回NO,因为它是异步的。我需要找到一种检查成功调用的方法,并根据此回调返回YESNO

有人能指出我如何编写等待成功通话的应用程序的正确方向,以便我知道用户尚未设置?

-(BOOL)doesTheUserExistAlreadyOnServer:(NSString *)parsedEmail
{
    BOOL *methodResponse = NO;

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.myurl.co.uk/"]];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"http://www.myurl.co.uk/igym.php"
                                                      parameters:@{@"myvar2":@"piggy"}];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text

      //  NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
        if ([[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] isEqualToString:@"piggy"]) {
            __block methodResponse = YES;
            NSLog(@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation start];

    return (BOOL)methodResponse;

}

修改

我使用以下逻辑解决了这个问题。

用户点击注册按钮。主要方法执行所有初步的非Web检查,然后调用[self doesTheUserExistAlreadyOnServer:_email.text];

该方法代码现在是

-(void)doesTheUserExistAlreadyOnServer:(NSString *)parsedEmail
{ 
    if(![_spinner isAnimating])
    {
        [_spinner startAnimating];
    }

    __block RegistrationViewController* me = self;
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.myurl.co.uk/"]];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"http://www.myurl.co.uk/igym.php"
                                                      parameters:@{@"myvar2":@"piggy"}];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text

        if ([[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] isEqualToString:@"piggy"]) {

            NSLog(@"%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
            [me registrationPartTwo:YES];
        } else
        {
            [me registrationPartTwo:NO];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation start];

}

然后,一旦该块/回调成功,它就会调用

-(void)registrationPartTwo:(BOOL)doesItExistOnServer
{
    [_spinner stopAnimating];
    NSString *emailAlreadyInUseMessage = [NSString stringWithFormat:@"This email is already in use"];
    if (doesItExistOnServer)
    {
        self.screenMsg.text = emailAlreadyInUseMessage;
//here more code to send the user the the next step
    }
}

基本上我使用依赖于回调的2方法注册过程来解决这个问题,不知道这是最好还是最有效的方法。但这就是我自己解决它的方式。

1 个答案:

答案 0 :(得分:0)

使用委托回调来通知操作何时完成。如果需要,在开始操作之前,请设置UIActivityIndicatorView(微调器)以防止用户与应用程序交互。