首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLConnection sendAsynchronousRequest:阻塞主线程

NSURLConnection sendAsynchronousRequest:阻塞主线程
EN

Stack Overflow用户
提问于 2013-04-21 03:36:40
回答 2查看 2.6K关注 0票数 2

我正在使用NSURLConnection发出多个异步请求。我想显示一个进度指示器,以显示在要执行的总请求数中已完成的请求数。但是,当我尝试在发出请求之前或在执行请求之前调用的另一个方法中设置和显示此进度指示器时,它将不会显示。当请求被注释掉时,进度指示器显示正常。但如果不是这样,Xcode就好像向前看,看到一个异步请求即将到来,并阻塞主线程,从而使UI更改变得不可能。

下面是被调用的相关代码,包括显示进度指示器的请求和代码:

代码语言:javascript
复制
- (void)getRegionalInformationFromChecked:(NSSet *)set atIndex:(NSInteger)index {
    __block BOOL responseRecieved = NO;
    NSString *stringForURL = [NSString stringWithFormat:@"http://www.thebluealliance.com/api/v1/event/details?event=%@",[[set allObjects] objectAtIndex:index]];
    NSURL *url = [NSURL URLWithString:stringForURL];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    NSLog(@"URL IS GO: %@", stringForURL);
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:queue completionHandler:^(NSURLResponse *_response, NSData *_data, NSError *_error) {
    NSLog(@"CHECKED DATA RETURNED AT INDEX %i", index);
    NSError *error;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:&error];
    if (!_regionalDetails) {
        _regionalDetails = [[NSMutableArray alloc] init];
    }
    [_regionalDetails addObject:dict];
    responseRecieved = YES;
}];
    regionalSchedulesToGet = [set count];
    while (responseRecieved == NO) {}
    [[MBProgressHUD HUDForView:[[UIApplication sharedApplication] keyWindow]] setLabelText:[NSString stringWithFormat: @"Getting regional %i of %i", index+2, [set count]]];
    if (index+1 < [set count]) {
        [self getRegionalInformationFromChecked:set atIndex:index+1];
    } else {
        [[MBProgressHUD HUDForView:[[UIApplication sharedApplication] keyWindow]] setLabelText:@"Writing to file"];
    }
}

当异步请求的块被注释掉时,MBProgressHUD会正常显示它的值。但是当插入块时,SDK拒绝更新进度指示器,即使在离开块之后(在此之后,任何线程问题都应该已经解决)。它不会更新,直到不再有要显示的请求,此时它显示为"Writing to file“。

为什么异步请求似乎阻塞了主线程,为什么我不能在请求被调用之前或之后立即在主线程上进行更改?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-21 03:56:42

使用

代码语言:javascript
复制
while (responseRecieved == NO) {}

您阻塞主线程(可能有几乎100%的CPU负载),直到异步块完成。然后递归地调用您的函数,启动另一个异步块,然后再次块,直到完成。因此,在所有操作完成之前,程序控制不会返回主运行循环。只有这样,才能完成UI更新。

您应该在完成块的末尾开始下一个操作,而不是同步等待(这总是一个坏主意)。

还要注意,sendAsynchronousRequestqueue参数是在其上调用完成处理程序的队列,因此您可以只使用[NSOperationQueue mainQueue]

然后,您的代码大致如下所示:

代码语言:javascript
复制
- (void)getRegionalInformationFromChecked:(NSSet *)set atIndex:(NSInteger)index
{
    [[MBProgressHUD HUDForView:[[UIApplication sharedApplication] keyWindow]]
     setLabelText:[NSString stringWithFormat:@"Getting regional %i of %i", index+1, [set count]]];
    NSString *stringForURL = [NSString stringWithFormat:@"http://www.thebluealliance.com/api/v1/event/details?event=%@",[[set allObjects] objectAtIndex:index]];
    NSURL *url = [NSURL URLWithString:stringForURL];
    NSLog(@"URL IS GO: %@", stringForURL);
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *_response, NSData *_data, NSError *_error) {
        NSLog(@"CHECKED DATA RETURNED AT INDEX %i", index);
        NSError *error;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:&error];
        if (!_regionalDetails) {
            _regionalDetails = [[NSMutableArray alloc] init];
        }
        [_regionalDetails addObject:dict];
        if (index+1 < [set count]) {
            [self getRegionalInformationFromChecked:set atIndex:index+1];
        } else {
            [[MBProgressHUD HUDForView:[[UIApplication sharedApplication] keyWindow]] setLabelText:@"Writing to file"];
            // ... perhaps call a completion function from here ?
        }
    }];
}

但请注意,对getRegionalInformationFromChecked的初始调用现在几乎会立即返回(这就是异步任务的工作方式:-)。

票数 5
EN

Stack Overflow用户

发布于 2013-04-21 03:49:35

尝试在主线程上分派涉及UI刷新的所有方法

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16124369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档