dispatch_async and block in iOS
That is a Grand Central Dispatch block.
- dispatch_async is a call to run on another queue.
- dispatch_get_global_queue is a call to get a specific queue with the desired characteristics. For example, the code could be run at a low priority on the DISPATCH_QUEUE_PRIORITY_BACKGORUND.
- Inside the block, the code does nothing. Post is set to nil. Then a message is sent to nil "dataUsingEncoding." Objective C drops all calls to nil. Finally, the parser is sent "nil" postData.
- At best, this will do nothing. At worst sending the parser nil data will crash.
The piece of code in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
is run asynchronously on a background thread. This is done because parsing data may be a time consuming task and it could block the main thread which would stop all animations and the application wouldn't be responsive.
If you want to find out more, read Apple's documentation on Grand Central Dispatch and Dispatch Queue.
If the above code snippets doesn't work then, try this:
Objective-C:
dispatch_async(dispatch_get_main_queue(), ^{
});
UI updates should always be executed from the main queue. The "^" symbol indicates a start of a block.
Swift 3:
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
}