iPhone – Grand Central Dispatch main thread

Dispatching a block to the main queue is usually done from a background queue to signal that some background processing has finished e.g. – (void)doCalculation { //you can use any string instead “com.mycompany.myqueue” dispatch_queue_t backgroundQueue = dispatch_queue_create(“com.mycompany.myqueue”, 0); dispatch_async(backgroundQueue, ^{ int result = <some really long calculation that takes seconds to complete>; dispatch_async(dispatch_get_main_queue(), ^{ [self … Read more

How do I wait for an asynchronously dispatched block to finish?

Trying to use a dispatch_semaphore. It should look something like this: dispatch_semaphore_t sema = dispatch_semaphore_create(0); [object runSomeLongOperationAndDo:^{ STAssert… dispatch_semaphore_signal(sema); }]; if (![NSThread isMainThread]) { dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); } else { while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]]; } } This should behave correctly even if runSomeLongOperationAndDo: decides that the operation isn’t actually long enough … Read more

Wait until swift for loop with asynchronous network requests finishes executing

You can use dispatch groups to fire an asynchronous callback when all your requests finish. Here’s an example using dispatch groups to execute a callback asynchronously when multiple networking requests have all finished. override func viewDidLoad() { super.viewDidLoad() let myGroup = DispatchGroup() for i in 0 ..< 5 { myGroup.enter() Alamofire.request(“https://httpbin.org/get”, parameters: [“foo”: “bar”]).responseJSON { … Read more

Waiting until two async blocks are executed before starting another block

Use dispatch groups: see here for an example, “Waiting on Groups of Queued Tasks” in the “Dispatch Queues” chapter of Apple’s iOS Developer Library’s Concurrency Programming Guide Your example could look something like this: dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block1 NSLog(@”Block1″); [NSThread sleepForTimeInterval:5.0]; NSLog(@”Block1 End”); }); dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // … Read more

In Swift how to call method with parameters on GCD main thread?

Modern versions of Swift use DispatchQueue.main.async to dispatch to the main thread: DispatchQueue.main.async { // your code here } To dispatch after on the main queue, use: DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code here } Older versions of Swift used: dispatch_async(dispatch_get_main_queue(), { let delegateObj = UIApplication.sharedApplication().delegate as YourAppDelegateClass delegateObj.addUIImage(“yourstring”) })

How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?

Since the beginning, Swift has provided some facilities for making ObjC and C more Swifty, adding more with each version. Now, in Swift 3, the new “import as member” feature lets frameworks with certain styles of C API — where you have a data type that works sort of like a class, and a bunch … Read more

How to create dispatch queue in Swift 3

Creating a concurrent queue let concurrentQueue = DispatchQueue(label: “queuename”, attributes: .concurrent) concurrentQueue.sync { } Create a serial queue let serialQueue = DispatchQueue(label: “queuename”) serialQueue.sync { } Get main queue asynchronously DispatchQueue.main.async { } Get main queue synchronously DispatchQueue.main.sync { } To get one of the background thread DispatchQueue.global(qos: .background).async { } Xcode 8.2 beta 2: … Read more

How do I write dispatch_after GCD in Swift 3, 4, and 5?

The syntax is simply: // to run something in 0.1 seconds DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code here } Note, the above syntax of adding seconds as a Double seems to be a source of confusion (esp since we were accustomed to adding nsec). That “add seconds as Double” syntax works because deadline … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)