ios - ASIHTTPRequest 导致 iPhone 5s 中的应用卡住

标签 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];

编辑 如何使用 block 使用默认 url 连接发布数据。

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

最佳答案

请删除 ASIFormDataRequest 上的weak 限定。命名暗示这应该不是问题,但有一种情况是请求立即被解除分配并为空。如果唯一的 UI 更新发生在完成 block 中,这可能会显示为挂起。

至少,那个弱限定词是不必要的。

如果 ASIFormDataRequest 保留该 block (它不应该,因为如果他们告诉您将其用于responseData,那么它会鼓励保留周期),您可以保留一个普通的指针,但以这种方式绕过循环:

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

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

关于ios - ASIHTTPRequest 导致 iPhone 5s 中的应用卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22470210/

相关文章:

ios - distinctUnionOfObjects 不工作

ios - 检查 textField.text isEqualToString

iphone - 添加由主音量控制的声音

ios - ASINetworkQueue正确使用和存储

iphone - 带有正文参数的 ASIHttpRequest DELETE 方法

ios - 无法创建 UIManagedDocument

ios - 占位符在构建时删除约束

iphone - 计时器:时间到时在另一个 View Controller 上看到警报

iphone - iPhone 中的增强现实可以显示 3d 模型

iphone - 解析 JSON 字典/数组