The user interface is blocked when using NSURLSession

I am working on a project that requires a registration form using a web service for authentication. I have no problem connecting to the server, but it seems that NSURLSession is blocking my user interface, and I really don't know why after a lot of debugging.

For simplicity, here is a short code:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"example.com/service"]];

//1
    //_sessionLogin = [NSURLSession sessionWithConfiguration:sessionConfigurationLogin delegate:self delegateQueue:nil];
//2 //Whether I use 1 or 2, it acts the same way

    _sessionLogin = [NSURLSession sharedSession];

    NSURLSessionDataTask *sessionDataTaskLogin = [_sessionLogin dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        if(!error)
        {
            NSLog(@"loginWithSuccess");
            UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:@"Login ok" message:@"ok" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [alertError show];

    }
}];

//Begin login request
[sessionDataTaskLogin resume];

_sessionLogin is NSURLSession

When the connection to my server is fast, NSLog (@ "loginWithSuccess") appears almost immediately after I clicked on the login button, but it takes me some time (long time) about 10 seconds, which UIAlertView showed. And I, too, cannot interact with the user interface.

Thanks in advance for each decision.

+4
1

. , , .

NSURLSessionDataTask *sessionDataTaskLogin = [_sessionLogin dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (!error) {
        NSLog(@"loginWithSuccess");
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:@"Login ok" message:@"ok" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [alertError show];
        });
    }
}];
+10

Source: https://habr.com/ru/post/1537096/


All Articles