dispatch-queue

Swift Async With Completion Block

China☆狼群 提交于 2019-12-01 01:20:43
I have two functions that executes async. I tried to "syncronize" them with: DispatchGroup and DispatchQueue let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent) let group = DispatchGroup() queue.async(group: group) { //func1 } queue.async(group: group) { //func2 } group.notify(queue: queue) { print("#3 finished") } Func1 and Func2 are only calls of: class func getDataFromUrl( url: URL, completion: @escaping ( Data?, URLResponse?, Error? ) -> ( ) ) { URLSession.shared.dataTask( with: url ) { data, response, error in completion( data, response, error ) }.resume( )

Stop a DispatchQueue that is running on the main thread

浪尽此生 提交于 2019-11-28 12:42:58
I have this block of code: DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay) { self.isShootingOnHold = false self.shoot() self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true) } Now, I want to be able to stop this thread from executing. How can I stop it from being executed? For instance, after 3 seconds, I decide I don't want that to execute anymore so I want to stop it. You can use DispatchWorkItem s. They

How to use background thread in swift?

本小妞迷上赌 提交于 2019-11-26 00:49:32
问题 How to use threading in swift? dispatchOnMainThread:^{ NSLog(@\"Block Executed On %s\", dispatch_queue_get_label(dispatch_get_current_queue())); }]; 回答1: Swift 3.0+ A lot has been modernized in Swift 3.0. Running something on the background thread looks like this: 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") } } Swift 1.2 through 2.3 let

How to use background thread in swift?

核能气质少年 提交于 2019-11-25 20:07:13
How to use threading in swift? dispatchOnMainThread:^{ NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue())); }]; Swift 3.0+ A lot has been modernized in Swift 3.0. Running something on the background thread looks like this: 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") } } Swift 1.2 through 2.3 let qualityOfServiceClass = QOS_CLASS_BACKGROUND let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass