iOS/Objective-C 相当于 Android 的 AsyncTask [英] iOS/Objective-C equivalent of Android's AsyncTask

查看:18
本文介绍了iOS/Objective-C 相当于 Android 的 AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉在Android中使用AsyncTask:创建一个子类,在子类的实例上调用execute,然后调用onPostExecute在 UI 线程或主线程上.iOS 中的等价物是什么?

I'm familiar with using AsyncTask in Android: create a subclass, call execute on an instance of the subclass and onPostExecute is called on the UI thread or main thread. What's the equivalent in iOS?

推荐答案

对原始问题的回答:

Grand Central Dispatch (GCD) 提供了一种在后台执行任务的机制,尽管它在结构上与 AsyncTask 不同.要异步执行某些操作,您只需要创建一个队列(如线程),然后将一个块传递给 dispatch_async() 以在后台执行.我发现它比 AsyncTask 更简洁,因为不涉及子类化;无论您有想要在后台执行的代码,它都或多或少是即插即用的.一个例子:

Answer to Original Question:

Grand Central Dispatch (GCD) offers a mechanism to perform tasks in the background, though it works in a structurally different way than AsyncTask. To perform something asynchronously, you just need to create a queue (like a thread) and then pass a block to dispatch_async() to be performed in the background. I find it neater than AsyncTask, as there is no subclassing involved; it is more or less plug-and-play wherever you have code you'd like to execute in the background. An example:

dispatch_queue_t queue = dispatch_queue_create("com.yourdomain.yourappname", NULL);
dispatch_async(queue, ^{
    //code to be executed in the background
});

其他要点:

1) 回调

如果您想在后台执行任务并在后台任务完成后更新 UI(或在另一个线程上做某事),您可以简单地嵌套调度调用:

Other Points:

1) Callbacks

If you want to perform a task in the background and update the UI (or do something on another thread) when the background task is done, you can simply nest the dispatch calls:

dispatch_queue_t queue = dispatch_queue_create("com.yourdomain.yourappname", NULL);
dispatch_async(queue, ^{
    //code to be executed in the background
    dispatch_async(dispatch_get_main_queue(), ^{
        //code to be executed on the main thread when background task is finished
    });
});

2) 全局队列

创建队列时,也可以使用dispatch_get_global_queue()函数来获取具有一定优先级的全局调度队列(如DISPATCH_QUEUE_PRIORITY_HIGH).这些队列是普遍可访问的,当您想将多个任务分配给同一个线程/队列时非常有用.请注意,内存完全由 iOS 为您管理.

2) Global Queues

When creating a queue, you can also use the dispatch_get_global_queue() function to get a global dispatch queue with a certain priority (such as DISPATCH_QUEUE_PRIORITY_HIGH). These queues are universally accessible and are useful when you want to assign multiple tasks to the same thread/queue. Note that memory is managed for you completely by iOS.

内存管理和调度队列有时会有些混乱,因为它们有自己的 dispatch_retain/dispatch_release 函数.但是,请放心,它们被 ARC 视为 Objective-C 对象,因此您无需担心调用这些函数.参考 rob mayoff 对 GCD 和 ARC 的精彩回答,您可以看到文档描述了 GCD 队列与 Objective-C 对象的等效性:

There is sometimes some confusion regarding memory management and dispatch queues because they have their own dispatch_retain/dispatch_release functions. However, rest assured that they are treated as Objective-C objects by ARC, so you don't need to worry about calling these functions. Referencing rob mayoff's great answer regarding GCD and ARC, you can see the documentation describe GCD queues' equivalence with Objective-C objects:

* By default, libSystem objects such as GCD and XPC objects are declared as
* Objective-C types when building with an Objective-C compiler. This allows
* them to participate in ARC, in RR management by the Blocks runtime and in
* leaks checking by the static analyzer, and enables them to be added to Cocoa
* collections.
*
* NOTE: this requires explicit cancellation of dispatch sources and xpc
*       connections whose handler blocks capture the source/connection object,
*       resp. ensuring that such captures do not form retain cycles (e.g. by
*       declaring the source as __weak).
*
* To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
* compiler flags.
*
* This mode requires a platform with the modern Objective-C runtime, the
* Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
* or iOS 6.0 deployment target.

4) 多个任务/块

我要补充一点,如果任务在多个异步活动完成之前无法继续,GCD 有一个分组接口支持同步多个异步块.Jörn Eyrich 和 ɲeuroburɳ 对此主题提供了慷慨的解释 这里.如果您需要此功能,我强烈建议您花几分钟仔细阅读他们的答案并了解它们之间的差异.

4) Multiple Tasks/Blocks

I'll add that GCD has a grouping interface supports synchronizing multiple asynchronous blocks if a task cannot continue until multiple asynchronous activities have completed. Jörn Eyrich and ɲeuroburɳ provide a generous explanation of this topic here. If you need this functionality, I would highly recommend taking a few minutes to read both of their answers closely and understand the differences between them.

文档 提供了大量有关该主题的信息.

The documentation has a wealth of information on the topic if you are so inclined.

这篇关于iOS/Objective-C 相当于 Android 的 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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