iOS中的dispatch_async和阻止

时间:2022-10-09 05:18:51

What this piece of code mean?

这段代码意味着什么?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        TMBaseParser *parser=[[TMBaseParser alloc] init];
        parser.delegate=self;
        NSString *post =nil;
        NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
        [parser parseForServiceType:TMServiceCategories postdata:postData];
    });

please explain it briefly.Thanks

请简要解释一下。谢谢

3 个解决方案

#1


98  

The piece of code in

这段代码

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

});

is run asynchronously on a background thread. This is done because parsing data may be a time consuming task and it could block the main thread which would stop all animations and the application wouldn't be responsive.

在后台线程上异步运行。这样做是因为解析数据可能是一项耗时的任务,并且它可以阻止主线程停止所有动画并且应用程序不会响应。

If you want to find out more, read Apple's documentation on Grand Central Dispatch

如果您想了解更多信息,请阅读Apple在Grand Central Dispatch上的文档

#2


5  

If the above code snippets doesn't work then, try this:

如果上面的代码片段不起作用,那么试试这个:

Objective-C:

Objective-C的:

dispatch_async(dispatch_get_main_queue(), ^{

});

UI updates should always be executed from the main queue. The "^" symbol indicates a start of a block.

应始终从主队列执行UI更新。 “^”符号表示块的开始。

Swift 3:

斯威夫特3:

DispatchQueue.global(qos: .background).async {
    print("This is run on the background queue")

    DispatchQueue.main.async {
        print("This is run on the main queue, after the previous code in outer block")
    }
}

#3


2  

That is a Grand Central Dispatch block.

那是Grand Central Dispatch街区。

  1. dispatch_async is a call to run on another queue.
  2. dispatch_async是在另一个队列上运行的调用。
  3. dispatch_get_global_queue is a call to get a specific queue with the desired characteristics. For example, the code could be run at a low priority on the DISPATCH_QUEUE_PRIORITY_BACKGORUND.
  4. dispatch_get_global_queue是一个获取具有所需特征的特定队列的调用。例如,代码可以在DISPATCH_QUEUE_PRIORITY_BACKGORUND上以低优先级运行。
  5. Inside the block, the code does nothing. Post is set to nil. Then a message is sent to nil "dataUsingEncoding." Objective C drops all calls to nil. Finally, the parser is sent "nil" postData.
  6. 在块中,代码什么都不做。帖子设置为零。然后将消息发送到nil“dataUsingEncoding”。 Objective C将所有调用都删除为nil。最后,解析器发送“nil”postData。
  7. At best, this will do nothing. At worst sending the parser nil data will crash.
  8. 充其量,这将无济于事。最坏的情况是发送解析器nil数据会崩溃。

#1


98  

The piece of code in

这段代码

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

});

is run asynchronously on a background thread. This is done because parsing data may be a time consuming task and it could block the main thread which would stop all animations and the application wouldn't be responsive.

在后台线程上异步运行。这样做是因为解析数据可能是一项耗时的任务,并且它可以阻止主线程停止所有动画并且应用程序不会响应。

If you want to find out more, read Apple's documentation on Grand Central Dispatch

如果您想了解更多信息,请阅读Apple在Grand Central Dispatch上的文档

#2


5  

If the above code snippets doesn't work then, try this:

如果上面的代码片段不起作用,那么试试这个:

Objective-C:

Objective-C的:

dispatch_async(dispatch_get_main_queue(), ^{

});

UI updates should always be executed from the main queue. The "^" symbol indicates a start of a block.

应始终从主队列执行UI更新。 “^”符号表示块的开始。

Swift 3:

斯威夫特3:

DispatchQueue.global(qos: .background).async {
    print("This is run on the background queue")

    DispatchQueue.main.async {
        print("This is run on the main queue, after the previous code in outer block")
    }
}

#3


2  

That is a Grand Central Dispatch block.

那是Grand Central Dispatch街区。

  1. dispatch_async is a call to run on another queue.
  2. dispatch_async是在另一个队列上运行的调用。
  3. dispatch_get_global_queue is a call to get a specific queue with the desired characteristics. For example, the code could be run at a low priority on the DISPATCH_QUEUE_PRIORITY_BACKGORUND.
  4. dispatch_get_global_queue是一个获取具有所需特征的特定队列的调用。例如,代码可以在DISPATCH_QUEUE_PRIORITY_BACKGORUND上以低优先级运行。
  5. Inside the block, the code does nothing. Post is set to nil. Then a message is sent to nil "dataUsingEncoding." Objective C drops all calls to nil. Finally, the parser is sent "nil" postData.
  6. 在块中,代码什么都不做。帖子设置为零。然后将消息发送到nil“dataUsingEncoding”。 Objective C将所有调用都删除为nil。最后,解析器发送“nil”postData。
  7. At best, this will do nothing. At worst sending the parser nil data will crash.
  8. 充其量,这将无济于事。最坏的情况是发送解析器nil数据会崩溃。