grand-central-dispatch

How does dispatch_set_target_queue work?

被刻印的时光 ゝ 提交于 2019-12-19 06:51:31
问题 Due to the lack of material on dispatch_set_target_queue , I have come here for help, so thanks! Here is my test code: dispatch_queue_t mySerialDispatchQueue1 = dispatch_queue_create("come.itenyh", NULL); dispatch_queue_t mySerialDispatchQueue2 = dispatch_queue_create("come.itenyh1", NULL); dispatch_set_target_queue(mySerialDispatchQueue1, mySerialDispatchQueue2); dispatch_async(mySerialDispatchQueue1, ^{[self task:@"s1"];}); dispatch_async(mySerialDispatchQueue2, ^{[self task:@"p1"];});

dispatch_after looped / repeated

心不动则不痛 提交于 2019-12-19 03:57:01
问题 I am trying to create a loop like this: while (TRUE){ dispatch_after(...{ <some action> }); } After a viewDidLoad. The idea is to repeat the dispatch_after repeatedly. The dispatch_after waits two seconds before doing the action. This does not work - the screen just blanks? Is it stuck in looping or ...? 回答1: The dispatch_after(...) call returns immediately no matter when it is scheduled to run. This means that your loop is not waiting two seconds between dispatching them. Instead you are

Asynchronously dispatched recursive blocks

不打扰是莪最后的温柔 提交于 2019-12-18 20:06:09
问题 Suppose I run this code: __block int step = 0; __block dispatch_block_t myBlock; myBlock = ^{ if(step == STEPS_COUNT) { return; } step++; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); }; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); The block is invoked once from outside. When the inner invocation is reached,

Asynchronously dispatched recursive blocks

大城市里の小女人 提交于 2019-12-18 20:06:03
问题 Suppose I run this code: __block int step = 0; __block dispatch_block_t myBlock; myBlock = ^{ if(step == STEPS_COUNT) { return; } step++; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); }; dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2); dispatch_after(delay, dispatch_get_current_queue(), myBlock); The block is invoked once from outside. When the inner invocation is reached,

Using Grand Central Dispatch in Swift to parallelize and speed up “for" loops?

旧城冷巷雨未停 提交于 2019-12-18 16:54:41
问题 I am trying to wrap my head around how to use GCD to parallelize and speed up Monte Carlo simulations. Most/all simple examples are presented for Objective C and I really need a simple example for Swift since Swift is my first “real” programming language. The minimal working version of a monte carlo simulation in Swift would be something like this: import Foundation import Cocoa var winner = 0 var j = 0 var i = 0 var chance = 0 var points = 0 for j=1;j<1000001;++j{ var ability = 500 var

How can I be notified when a dispatch_async task is complete?

心不动则不痛 提交于 2019-12-18 15:08:33
问题 I have a asynchronous task like so: dispatch_async(dispatch_get_main_queue(), ^{ myAsyncMethodsHere; }); Is there a way to be notified when the background task is complete? Or to call a method upon completion? I've read through the documentation and have looked into dispatch_after, but it seems to be more designed to dispatch the method after a certain length of time. Thanks for the help. 回答1: From the docs: COMPLETION CALLBACKS Completion callbacks can be accomplished via nested calls to the

Suggested resources for learning about blocks

允我心安 提交于 2019-12-18 13:23:17
问题 What are some good suggested resources for learning about blocks and GCD in Mac OS X and iOS 回答1: I would start with Apple's Blocks Programming Topics document or with Programming with Blocks on Apple Devices. As gs mentioned, also check out Mike Ash's articles (and also subscribe to his RSS feed, as I'm sure more blocks-related posts will come up): Friday Q&A 2008-12-26 Friday Q&A 2009-08-14: Practical Blocks Friday Q&A 2009-08-28: Intro to Grand Central Dispatch Part 1 回答2: There is a great

What happens to Dispatch Queues when UIViewController is Deallocated?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 11:42:42
问题 I am trying to better understand retain cycles, especially relative to Dispatch Queues. I am working with AVFoundation and managing an AVCaptureSession on a sessionQueue: private let sessionQueue = DispatchQueue(label: "com.andrewferrarone.sessionQueue") in a lot of code examples in the apple documentation I see this: self.sessionQueue.async { [unowned self] // } Is the [unowned self] self here necessary? self (the viewController) references self.sessionQueue and the closure dispatched to

Asynchronous url requests inside dispatch_async

青春壹個敷衍的年華 提交于 2019-12-18 11:15:12
问题 I am trying to implement asynchronous url requests in a particular function, I want all these requests to complete and then do a particular action but the action precedes the requests i.e, it is getting called before the requests complete. dispatch_queue_t fetchQ = dispatch_queue_create("Featured Doc Downloader", NULL); dispatch_async(fetchQ, ^{ [self myAsyncMultipleURLRequestFunction]; dispatch_sync(dispatch_get_main_queue(), ^{ [self updateUIFunction]; }); }); -(void

Get current dispatch queue?

天涯浪子 提交于 2019-12-18 10:59:16
问题 I have a method which should support being called from any queue, and should expect to. It runs some code in a background thread itself, and then uses dispatch_get_main_queue when it returns a value to its block argument. I don't want it to force it onto the main queue if it wasn't when it entered the method. Is there a way to get a pointer to the current dispatch queue? 回答1: You do have the option of "dispatch_get_current_queue()", however the iOS 6.1 SDK defines this API with these