ios - 使用 block 会自动创建新线程吗?

标签 ios multithreading objective-c-blocks

刚开始使用块进行工作……非常困惑。我正在使用这样的块:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *myDictionary = [[mySingleton arrayPeopleAroundMe] objectAtIndex:indexPath.row];

NSMutableString *myString = [[NSMutableString alloc] initWithString:@"http://www.domain.com/4DACTION/PP_profileDetail/"];
[myString appendString:[myDictionary objectForKey:@"userID"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]
                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                        timeoutInterval:60.0];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
 sendAsynchronousRequest:urlRequest
 queue:queue
 completionHandler: ^( NSURLResponse *response,
                      NSData *data,
                      NSError *error)
 {
     [[mySingleton dictionaryUserDetail] removeAllObjects];
     [[mySingleton arrayUserDetail] removeAllObjects];

     if ([data length] > 0 && error == nil) // no error and received data back
     {
         NSError* error;
         NSDictionary *myDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
         [mySingleton setDictionaryUserDetail:[myDic mutableCopy]];

         NSArray *myArray = [myDic objectForKey:@"searchResults"];
         [mySingleton setArrayUserDetail:[myArray mutableCopy]];

         [self userDetailComplete];
     } else if
         ([data length] == 0 && error == nil) // no error and did not receive data back
     {
         [self serverError];
     } else if
         (error != nil) // error
     {
         [self serverError];
     }
 }];
}

连接完成后,将称为:
-(void)userDetailComplete {
ViewProfile *vpVC = [[ViewProfile alloc] init];
[vpVC setPassedInstructions:@"ViewDetail"];
[[self navigationController] pushViewController:vpVC animated:YES];
}

导致此错误弹出:
“试图从主线程或Web线程以外的线程获取Web锁。这可能是从辅助线程调用UIKit的结果。”
我摆脱错误的唯一方法是将userDetailComplete更改为此:
-(void)userDetailComplete {
dispatch_async(dispatch_get_main_queue(), ^{
ViewProfile *vpVC = [[ViewProfile alloc] init];
[vpVC setPassedInstructions:@"ViewDetail"];
[[self navigationController] pushViewController:vpVC animated:YES];
});
}

我的问题:每次使用块时都会自动启动一个新线程吗?使用积木时,我还应该注意其他陷阱吗?

最佳答案

块不创建线程。它们是闭包;它们只包含可在将来某个时间运行的可运行代码。

这是在后台线程上运行的,因为这是您要求它执行的操作:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
  sendAsynchronousRequest:urlRequest
  queue:queue
  ...

您创建了一个新队列,然后要求NSURLConnection在该队列上回叫您。如果要在主线程上被调用,请传递[NSOperationQueue mainQueue]。通常这就是您想要的。

关于ios - 使用 block 会自动创建新线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15844264/

相关文章:

ios - 快速背景颜色动画循​​环

ios - 配置文件不包括应用程序标识符和钥匙串(keychain)访问组权利

iphone - 星星什么时候出现在 AppStore 列表中?

django - Redis队列与在apache多线程上运行的django web应用程序一起使用

android - Android-将不确定的ProgressBar与另一个线程一起使用

ios - 没有返回值的 block

iphone - UITextField 上的键盘没有被关闭

linux - 为什么在这种情况下会发生优先级反转 - Linux?

ios - Swift 将 AnyObject 转换为 block

objective-c - 声明一个 block 方法参数而不使用 typedef