0

我最近将 MDProgressHUD 添加到我的应用程序中,并按照自述文档中的说明进行操作,该文档建议我在主队列上设置 HUD,然后在新线程上执行其他任务,这就是我实现它的方式。在应用程序的初始启动时一切正常,但是如果您选择手机上的主页按钮,以便将应用程序置于后台,然后选择应用程序的图标将其恢复到前台,则应用程序将崩溃。

我已经实现了如下代码。当用户选择 OK 按钮时,应用程序将通过调用 Web 服务 (NSURLConnection) 来验证他们的登录 ID 和密码,即 authenticateUser。

- (IBAction)Ok:(id)sender {

    [self.txtPassword resignFirstResponder];

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
       [self authenticateUser];
       [MBProgressHUD hideHUDForView:self.view animated:YES];
    });

}

- (void)authenticateUser {

   self.loginEmailAddress = self.txtEmailAddress.text;
   self.loginPassword = self.txtPassword.text;

   if ([self.loginEmailAddress isEqualToString:@""] || [self.loginPassword isEqualToString:@""]) {
    self.lblErrMsg.text = @"Invalid login details. Please try again.";
    self.lblErrMsg.hidden = NO;
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    return;
}


    NSString *myRequestString = [NSString stringWithFormat:@"org_id=%@&login_id=%@&pword=%@", signedUpOrgId, self.loginEmailAddress, self.loginPassword];
    NSString *myEncodedRequestString = [myRequestString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

    NSData *myRequestData = [myEncodedRequestString dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:serviceURL
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                    timeoutInterval:60.0];


    [theRequest setHTTPMethod: @"POST"];
    [theRequest setHTTPBody: myRequestData];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        receivedData = [NSMutableData data];

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry! Can't connect." message:@"Sorry, but there seems to be a problem connecting to the internet at the moment. Please try again or wait until you have better reception for a data connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }
}

日志:

2012-07-17 23:40:33.699 AppName[6155:707] -[ContactListViewController authenticateUser]: unrecognized selector sent to instance 0x2616e0
2012-07-17 23:40:33.704 AppName[6155:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ContactListViewController authenticateUser]: unrecognized selector sent to instance 0x2616e0'
*** First throw call stack:
(0x3146588f 0x377a3259 0x31468a9b 0x31467915 0x313c2650 0x31d78933 0x31439a33 0x31439699 0x3143826f 0x313bb4a5 0x313bb36d 0x33435439 0x30b1ecd5 0xe13c7 0xe136c)
terminate called throwing an exception

我无法弄清楚为什么该应用程序第二次崩溃。任何帮助将不胜感激。

4

1 回答 1

0

在块中引用self时,建议通过“弱”变量间接执行此操作(否则您可能永远无法正确释放)。

__weak ContactListViewController *weakself = self; dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [weakself authenticateUser]; [MBProgressHUD hideHUDForView:weakself.view animated:YES]; });

这是因为对您的引用绑定到该块;它有一个重要的含义:你确定你在这个街区呆的时间足够长,可以很好地参考你吗?您可能需要仔细考虑并确保self被称为“属于”其他人的对象足够长的时间以仍然是执行此块的有效实例。

于 2012-07-17T14:46:25.883 回答