与多线程相比,Objective-C 中的同步和异步调用有什么区别? [英] What's the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

查看:21
本文介绍了与多线程相比,Objective-C 中的同步和异步调用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长期以来,我认为异步是在后台线程上运行某些东西的同义词,而同步则是在主线程上运行(阻止 UI 更新和交互).我理解不运行在主线程上进行昂贵的操作是因为它不允许在主线程被占用时发生 UI 操作,但为什么同步麻烦?

For the longest time I thought asynchronous was synonymous to running something on a background thread, while synchronous meant on the main thread (blocking UI updates and interactions). I understand that not running on the main thread for expensive actions is because it doesn't allow UI actions to occur as the main thread is occupied, but why is synchronous troublesome?

然而,我注意到你可以在主线程上进行异步调用,在后台线程上进行同步调用.

However, it's since came to my attention that you can make asynchronous calls on the main thread, and synchronous calls on background threads.

我总是听到人们说不要同步或在主线程上使用昂贵的调用,因为它会阻塞用户的 UI.这两个独立的问题是我应该确保不做的吗?有什么区别?

I always hear people saying not to use expensive calls synchronously or on the main thread, as it will block the UI for the user. Are these two separate issues I should be making sure I don't do? What are the differences?

推荐答案

当您同步调用某个东西时,这意味着启动该操作的线程将等待任务完成后再继续.异步意味着它不会等待.

When you invoke something synchronously, it means that the thread that initiated that operation will wait for the task to finish before continuing. Asynchronous means that it will not wait.

话虽如此,当人们建议您异步执行一些缓慢或昂贵的过程时,他们暗示不仅应该异步运行它,而且还应该在后台线程上执行此操作.目标是释放主线程,以便它可以继续响应用户界面(而不是冻结),因此您将任务异步分派到后台线程.

Having said that, when people suggest that you perform some slow or expensive process asynchronously, they are implicitly suggesting not only that you should run it asynchronously, but that you should do that on a background thread. The goal is to free the main thread so that it can continue to respond to the user interface (rather than freezing), so you are dispatching tasks to a background thread asynchronously.

所以,有两个部分.首先,以GCD为例,你抓取一个后台队列(要么抓取一个全局后台队列,要么创建自己的):

So, there are two parts to that. First, using GCD as an example, you grab a background queue (either grab one of the global background queues, or create your own):

// one of the global concurrent background queues

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// or you could create your own serial background queue:
//
// dispatch_queue_t queue = dispatch_queue_create("com.domain.app.queuename", 0);

其次,您将任务异步分派到该队列:

Second, you dispatch your tasks to that queue asynchronously:

dispatch_async(queue, ^{
    // the slow stuff to be done in the background
});

操作队列的模式非常相似.创建一个操作队列并将操作添加到该队列中.

The pattern for operation queues is very similar. Create an operation queue and add operations to that queue.

实际上,同步与异步的区别与主队列与后台队列的区别完全不同.但是当人们谈论异步运行一些慢速进程"时,他们实际上是在说在后台队列上异步运行一些慢速进程".

In reality, the synchronous vs asynchronous distinction is completely different from the main queue vs background queue distinction. But when people talk about "run some slow process asynchronously", they're really saying "run some slow process asynchronously on a background queue."

这篇关于与多线程相比,Objective-C 中的同步和异步调用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆