Question

I want to display SVProgressHUD during login process. But SVProgressHUD still waits after login. It's dismissed ~15seconds after "Login success" message. Here is my code:

[SVProgressHUD show];

__block BOOL result;
dispatch_async(queue, ^{

    result = [self autanticate];
    NSLog(@"autantication result = %d", result);
    result = [self getCSRFToken];
    NSLog(@"Login success result = %d", result);

    [SVProgressHUD dismiss];
});

autanticate and getCSRFToken functions are like below:

- (BOOL) getCSRFToken
{
__block BOOL success = false;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// Make sure that the callbacks are not called from the main queue, otherwise we would deadlock

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer  serializer];

NSDictionary *parameters = @{@"username": self.username.text,
                             @"password": self.password.text};

NSLog(@"Creating CSRF semaphore");
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

// CSRF Token bilgisini alalım
NSString *Url = [NSString stringWithFormat:@"%@%@", BaseURLString, @"?q=services/session/token"];
[manager GET:Url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"CSRF Token: %@", responseObject);
    success = true;
    dispatch_semaphore_signal(semaphore);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    success = false;
    dispatch_semaphore_signal(semaphore);

}];
NSLog(@"Waiting CSRF semaphore");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

return success;

}

- (BOOL)autanticate
{
__block BOOL isAutanticated = false; 
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer  serializer];

NSDictionary *parameters = @{@"username": self.username.text,
                             @"password": self.password.text};

// Session bilgilerini alalım

NSLog(@"Creating autantication semaphore");
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

NSString *Url = [NSString stringWithFormat:@"%@%@", BaseURLString, @"?q=rest/mserv/signin"];
[manager POST:Url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON: %@", responseObject);

    NSDictionary *result = [responseObject objectForKey:@"result"];
    NSString *sessid = [result objectForKey:@"sessid"];
    NSString *session_name = [result objectForKey:@"session_name"];
    NSLog(@"sessid = %@\nsession_name=%@", sessid, session_name);
    isAutanticated = true;
    dispatch_semaphore_signal(semaphore);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    isAutanticated = false;
    dispatch_semaphore_signal(semaphore);

}];

NSLog(@"Waiting autanticatio semaphore");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

return isAutanticated;

}
Was it helpful?

Solution

You must dispatch [SVProgressHUD dismiss] to the main thread

[SVProgressHUD show];
__block BOOL result;
dispatch_async(queue, ^{
    result = [self autanticate];
    NSLog(@"autantication result = %d", result);
    result = [self getCSRFToken];
    NSLog(@"Login success result = %d", result);
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    })
});

All user interface elements must only be modified on the main thread.

Remark: Note that the dispatch_async() returns (almost) immediately, so that the result is not set when dispatch_async() returns. And don't be tempted to use a semaphore to wait for the block to finish. That is fine on a background thread, but will most probably give a deadlock on the main thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top