2014-01-11 44 views
1

我希望在登錄過程中顯示SVProgressHUD。但SVProgressHUD登錄後仍然在等待。它在「登錄成功」消息後被解僱~15seconds。 這裏是我的代碼:SVProgressHud等得太久駁回

[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]; 
}); 

autanticategetCSRFToken功能是象下面這樣:

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

} 

回答

9

您必須派遣[SVProgressHUD dismiss]主線程

[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]; 
    }) 
}); 

只能在主線程上修改所有用戶界面元素。

注:注意,dispatch_async()回報 (幾乎)立即,使01​​是設置 時dispatch_async()回報。 不要試圖用信號量等待塊完成。 這是在後臺線程罰款,但很可能會給死鎖 主線程上。

+0

謝謝馬丁。 – zontragon

+0

感謝!它節省了這一天! –