gpt4 book ai didi

ios - SVProgressHud 等待时间过长才关闭

转载 作者:行者123 更新时间:2023-12-01 17:22:59 25 4
gpt4 key购买 nike

我要显示SVProgressHUD在登录过程中。但是SVProgressHUD登录后仍在等待。它被解雇了~15seconds在“登录成功”消息之后。
这是我的代码:

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

}

最佳答案

您必须发送 [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()返回
(几乎)立即,所以 result未设置
dispatch_async()返回。
并且不要试图使用信号量来等待 block 完成。
这在后台线程上很好,但很可能会导致死锁
在主线程上。

关于ios - SVProgressHud 等待时间过长才关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com