是什么在Objective-C的同步和异步调用,相对于多线程之间的区别? [英] What's the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

查看:154
本文介绍了是什么在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.

我总是听到人们说没有同步或主线程中使用昂贵的调用,因为它会阻止用户界面。这些是两个不同的问题,我应该确保我不这样做?有什么区别?

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.

在现实中,同步和一步区别是从主队列VS背景队列区分完全不同。但是,当人们谈论异步运行一些缓慢的过程,他们其实是在说以异步方式在后台运行队列一些缓慢的过程。

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天全站免登陆