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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
There are no classes for that in iOS but you can simulate it using queues. You can call:
for async tasks and inside your async code call next queue to do something in the view...:
Then, using this two queues you can create a asyncTask class, add this class to your project to implement them:
This is an example that I am using in a project:
And the following call:
You can add a
publishProgress()
update method and respective... I don't use it for the moment because I call my async task in background services.I hope it's helpful.
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: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:
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 asDISPATCH_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.3) Memory
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: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.
Swift 3
In Android when I wanted to run a task on a background thread and then update the UI when it finished, I used
AsyncTask
(example). Now when I am making iOS versions of my apps, I use Grand Central Dispatch (GCD) to do the same thing. Here is how it is done with Swift:Notes
if your targeting earlier iOS Version (than iOS 4 for Grand Central Dispatch) you could use the NSObject performSelector methods
Execute on Background Thread performSelectorInBackground:withObject:
And execute on MainThread performSelectorOnMainThread:withObject:waitUntilDone:
This is an example: