Question

I'm using ASITHTTPREQUEST for web service calling. Now app is freezing in iPhone5s device(iOS7.1). App is freezing in client device so I can not figure out the exact problem. Note that app is not crashing at all.

Code:

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

Edit How can I post data using default url connection using block.

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

Solution

Please remove the weak qualification on the ASIFormDataRequest. The naming implies that this shouldn't be a problem, but there's a scenario where the request is immediately deallocated and nulled. This might appear as a hang if the only UI update happens in the completion blocks.

At the very least, that weak qualifier is unnecessary.

If the ASIFormDataRequest retains the block (which it shouldn't, because if they tell you to use it for responseData, then it's encouraging a retain cycle), you can keep a normal pointer, but get around the cycle this way:

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

// replace all references to 'request' inside the block with 'weakRequest'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top