AFnetworking waitUntilFinish not working

余生颓废 提交于 2019-12-06 15:52:18

If you do this

return _loginstatus;

You are not waiting the operation to finish, so you're not returning what your server is sending to your application. What I usually do in this cases is display an UIActivityIndicator when the login action is fired, and do nothing till' the server has responded. Then, I set the proper values to any variables and in this case I would segue to home screen.

Remember that when you're using AFNetworking you are always doing async stuff.

EDIT

Code example:

- (void)loginActionWithPassword:(NSString *)pass
{
    // Add progress HUD to show feedback
    // I'm using MBProgressHUD library: https://github.com/jdg/MBProgressHUD#readme
    self.progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.progressHUD.labelText = @"Loading";
    self.progressHUD.mode = MBProgressHUDModeIndeterminate;
    self.progressHUD.dimBackground = YES;

    // Launch the action to the server
    [Actions loginWithEmail:[self.textEmail.text lowercaseString] andPassword:pass success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if ([Actions requestFailed:JSON]) {
            LOG(@"ERROR %@ / %@ / %@ \n", pass, request, response, JSON);
            if ([JSON[@"MSG"] isEqualToString:@"USER_NOT_REGISTERED"]) {
                self.progressHUD.labelText = @"User doesn't exist";
                self.textEmail.text = @"";
                self.textPassword.text = @"";
            } else if ([JSON[@"MSG"] isEqualToString:@"PASSWORD_INCORRECT"]) {
                self.progressHUD.labelText = @"Wrong password";
                self.textPassword.text = @"";
            }
            [self.progressHUD hide:YES afterDelay:[Utils hudDuration]];
            return;
        }
        // If everything went OK, go to this function to save user data
        // and perform segue to home screen (or dismiss modal login view)
        [self onLoginSuccessWithPassword:pass JSON:JSON response:response];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        // If something fails, display an error message
        LOG(@"ERROR:(%@): %@ / %@ / %@ / %@ \n", pass, request, response, error, JSON);
        self.progressHUD.labelText = @"Something went wrong, try again";
        [self.progressHUD hide:YES afterDelay:[Utils hudDuration]];
        return;
    }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!