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

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

问题描述

我熟悉使用的AsyncTask 在Android中:创建一个子类,叫执行上的一个实例子类和 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?

推荐答案

大中央调度(GCD)提供在后台执行任务的方式类似。要异步执行的东西,你只需要创建一个队列(如线程),然后通过一个块将在后台执行。

Answer to Original Question:

Grand Central Dispatch (GCD) offers a similar way of performing tasks in the background. To perform something asynchronously, you just need to create a queue (like a thread) and then pass a block to be performed in the background.

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

的<一个href="https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/doc/uid/TP40008079"相对=nofollow>文档对这一主题的更多信息,如果您愿意的话。

The documentation has a lot more information on the topic if you are so inclined.

如果你想在后台执行任务,并更新用户界面(或做一些事情在另一个线程),当它完成,你可以简单地嵌套像这样的调度电话:

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

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.

有时有关于内存管理和调度队列,因为他们有自己的保留 / 发布函数有些混乱。不过,放心,他们的待遇完全是Objective-C的对象由ARC,所以你不必担心调用这些函数。引用抢mayoff最伟大的答案关于GCD和ARC,你可以看到文档描述GCD队列等效性Objective-C的对象:

There is sometimes some confusion regarding memory management and dispatch queues because they have their own retain/release functions. However, rest assured that they are treated completely 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.

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

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