Saturday 17 October 2015

IOS: Executing a function in main queue using swift

      Sometimes our application will have multi queues, if so then you will have to be more careful about running your code on respective queues since a code that has to be run on a main queue wont run on another queues; mainly background queues. In this tutorial you're gonna learn how to send the codes to main queue from an another in swift for your ios app. 

      If you want to achieve something in your main queue(thread) from a background queue add the below code to you background queue. consider a case that you wont be able to show alert (which has to run on a main queue) from the dataTaskWithRequest() as it works on an another queue and here you have to call the alert from inside the below code.

Swift:
dispatch_async(dispatch_get_main_queue()) { () -> Void in  
 // add your code here  
 }  

Its really simple with swift isn't it!

No comments:

Post a Comment