grand-central-dispatch

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

怎甘沉沦 提交于 2019-11-26 03:17:59
问题 I am testing some code that does asynchronous processing using Grand Central Dispatch. The testing code looks like this: [object runSomeLongOperationAndDo:^{ STAssert… }]; The tests have to wait for the operation to finish. My current solution looks like this: __block BOOL finished = NO; [object runSomeLongOperationAndDo:^{ STAssert… finished = YES; }]; while (!finished); Which looks a bit crude, do you know a better way? I could expose the queue and then block by calling dispatch_sync :

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

狂风中的少年 提交于 2019-11-26 02:04:15
问题 In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // your function here }) But this no longer seems to compile since Swift 3. What is the preferred way to write this in modern Swift? 回答1: The syntax is simply: // to run something in 0.1 seconds DispatchQueue.main.asyncAfter(deadline: .now()

Wait until swift for loop with asynchronous network requests finishes executing

青春壹個敷衍的年華 提交于 2019-11-26 00:56:52
问题 I would like a for in loop to send off a bunch of network requests to firebase, then pass the data to a new view controller once the the method finishes executing. Here is my code: var datesArray = [String: AnyObject]() for key in locationsArray { let ref = Firebase(url: \"http://myfirebase.com/\" + \"\\(key.0)\") ref.observeSingleEventOfType(.Value, withBlock: { snapshot in datesArray[\"\\(key.0)\"] = snapshot.value }) } // Segue to new view controller here and pass datesArray once it is

NSOperation vs Grand Central Dispatch

。_饼干妹妹 提交于 2019-11-26 00:38:53
问题 I\'m learning about concurrent programming for iOS. So far I\'ve read about NSOperation/NSOperationQueue and GCD. What are the reasons for using NSOperationQueue over GCD and vice versa? Sounds like both GCD and NSOperationQueue abstract away the explicit creation of NSThreads from the user. However the relationship between the two approaches isn\'t clear to me so any feedback to appreciated! 回答1: GCD is a low-level C-based API that enables very simple use of a task-based concurrency model.

How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

雨燕双飞 提交于 2019-11-26 00:34:43
问题 Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int / double / float ? 回答1: I think you're looking for dispatch_after() . It requires your block to accept no parameters, but you can just let the block capture those variables from your local scope instead. int parameter1 = 12; float parameter2 = 144.1; // Delay execution of my block for 10 seconds. dispatch_after(dispatch_time(DISPATCH_TIME_NOW

How to create dispatch queue in Swift 3

谁都会走 提交于 2019-11-25 23:45:28
问题 In Swift 2, I was able to create queue with the following code: let concurrentQueue = dispatch_queue_create(\"com.swift3.imageQueue\", DISPATCH_QUEUE_CONCURRENT) But this doesn\'t compile in Swift 3. What is the preferred way to write this in Swift 3? 回答1: 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

Create singleton using GCD's dispatch_once in Objective-C

一世执手 提交于 2019-11-25 23:13:07
问题 If you can target iOS 4.0 or above Using GCD, is it the best way to create singleton in Objective-C (thread safe)? + (instancetype)sharedInstance { static dispatch_once_t once; static id sharedInstance; dispatch_once(&once, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } 回答1: This is a perfectly acceptable and thread-safe way to create an instance of your class. It may not technically be a "singleton" (in that there can only ever be 1 of these objects), but as long as

dispatch_after - GCD in Swift?

时间秒杀一切 提交于 2019-11-25 21:42:41
问题 I\'ve gone through the iBook from Apple, and couldn\'t find any definition of it: Can someone explain the structure of dispatch_after ? dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>) 回答1: A clearer idea of the structure: dispatch_after(when: dispatch_time_t, queue: dispatch_queue_t, block: dispatch_block_t?) dispatch_time_t is a UInt64 . The dispatch_queue_t is actually type aliased to an NSObject , but you should just use your familiar