ASIHTTPRequest导致iPhone5s中的应用程序冻结

时间:2014-03-18 04:10:16

标签: ios iphone asihttprequest nsjsonserialization

我使用ASITHTTPREQUEST进行Web服务调用。现在应用程序在iPhone5s设备(iOS7.1)中冻结。应用程序在客户端设备中冻结,所以我无法弄清楚确切的问题。请注意,应用程序根本没有崩溃。

代码:

if ( ![self connected]){

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No internet connection." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
    return;

}


// register
NSString *registerAPI = @"http://letsnurture.co.uk/demo/q&a/web-services/user-register.php";

NSURL *url = [NSURL URLWithString:registerAPI];
__weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request setPostValue:self.edFirstname.text forKey:@"firstname"];
[request setPostValue:self.edLastname.text forKey:@"lastname"];
[request setPostValue:self.edEmail.text forKey:@"email"];
[request setPostValue:self.edPassword1.text forKey:@"password"];
[request setPostValue:@"null" forKey:@"deviceid"];
[request setPostValue:@"true" forKey:@"check"];
[request setTimeOutSeconds:10];

[request setCompletionBlock:^{

    // Use when fetching text data
    NSString *responseString = [request responseString];

    NSLog(@"Reponse : %@",responseString);

    // Use when fetching binary data
    NSData *responseData = [request responseData];

    NSError *e;

    NSDictionary *JSON =
    [NSJSONSerialization JSONObjectWithData: responseData
                                    options: NSJSONReadingMutableContainers
                                      error: &e];

    int status = [[JSON valueForKey:@"status"] intValue];

    if (status == 1) {

       // success from server

    }else{

       // failed on server. show message from server in alert
        NSDictionary *user = [JSON valueForKey:@"User"];
        NSString *message = [user valueForKey:@"message"];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil ];
        [alert show];


    }

}];
[request setFailedBlock:^{

     [[[UIAlertView alloc]initWithTitle:@"Error" message:@"Can not connect to the internet." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];

    NSError *error = [request error];
    NSLog(@"Error : %@",error);

}];

[request startAsynchronous];

修改  如何使用块使用默认URL连接发布数据。

- (BOOL)connected
{
    Reachability *hostReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [hostReachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}

1 个答案:

答案 0 :(得分:1)

请删除ASIFormDataRequest上的weak资格。命名意味着这应该不是问题,但是有一种情况是请求立即被释放并且无效。如果在完成块中发生唯一的UI更新,则这可能显示为挂起。

至少,弱的限定符是不必要的。

如果ASIFormDataRequest保留了块(它不应该,因为如果它们告诉你将它用于responseData,那么它鼓励一个保留周期),你可以保持一个正常的指针,但这样绕过循环:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *weakRequest = request;

// replace all references to 'request' inside the block with 'weakRequest'