grand-central-dispatch

How to stop the execution of tasks in a dispatch queue?

耗尽温柔 提交于 2019-11-28 05:39:12
If I have a serial queue, how can I, from the main thread, tell it to immediately stop execution and cancel all of its tasks? There is no way to empty pending tasks from a dispatch queue without implementing non-trivial logic yourself as of iOS 9 / OS X 10.11. If you have a need to cancel a dispatch queue, you might be better off using NSOperationQueue which offers this and more. For example, here's how you "cancel" a queue: NSOperationQueue* queue = [NSOperationQueue new]; queue.maxConcurrentOperationCount = 1; // make it a serial queue ... [queue addOperationWithBlock:...]; // add operations

Perform UI Changes on main thread using dispatch_async or performSelectorOnMainThread? [duplicate]

。_饼干妹妹 提交于 2019-11-28 05:31:29
Possible Duplicate: Grand Central Dispatch (GCD) vs. performSelector - need a better explanation To execute "stuff" on the main thread, should I use dispatch_async or performSelectorOnMainThread ? Is there a preferred way, right/or wrong, and/or best practice? Example: I'm performing some logic within the block of an NSURLConnection sendAsynchronousRequest:urlRequest method. Because I'm doing stuff to the main view such as presenting a UIAlertView I need to show the UIAlertView on the main thread. To do this I'm using the following code. [NSURLConnection sendAsynchronousRequest:urlRequest

Possible to reset state of dispatch_once in unit test, to make them run again

↘锁芯ラ 提交于 2019-11-28 05:18:34
Is it possible to reset the state of dispatch_once code in a unit test tearDown? I think it would be nice if our unit tests could run from a really clean state, but we are struggling with dispatch_once and some singletons made with dispatch once. Josh Caswell I should note first that this isn't a good thing to do in any situation other than testing; even then, proceed with care -- AliSoftware provides some details and example code in comments below. See also the interesting answers at Can I declare a dispatch_once_t predicate as a member variable instead of static? , including some important

What are the tradeoffs between performSelector:withObject:afterDelay: and dispatch_after

こ雲淡風輕ζ 提交于 2019-11-28 05:01:33
The only functional difference I have encountered is that I can cancel the message scheduled with performSelector:withObject:afterDelay: . I don't know of a way to cancel a block submitted to dispatch_after . (Please let me know if there is a way to do this that I do not know about). I'd like to know more about: functional tradeoffs (What else can be accomplished with one interface but not the other?) performance tradeoffs (Is one implementation more efficient? In which cases?) stylistic tradeoffs (Should I prefer one interface for certain tasks to better follow common styles or conventions?)

Grand Central Dispatch async vs sync

隐身守侯 提交于 2019-11-28 04:57:10
I'm reading the docs on dispatch queues for GCD, and in it they say that the queues are FIFO, so I am woundering what effect this has on async / sync dispatches? from my understand async executes things in the order that it gets things while sync executes things serial.. but when you write your GCD code you decide the order in which things happen.. so as long as your know whats going on in your code you should know the order in which things execute.. my questions are, wheres the benefit of async here? am I missing something in my understanding of these two things. sync means the function WILL

dispatch_async vs. dispatch_sync using Serial Queues in Grand Central Dispatch

不问归期 提交于 2019-11-28 04:33:03
OK, I love Grand Central Dispatch and after using it with relative success but this is something I don't fully understand. Suppose I have created my own serial queue using dispatch_queue_t myQueue; myQueue = dispatch_queue_create("myQueue", NULL); After that I do this: dispatch_async(myQueue, ^{ [self doStuff1]; }); // and a few lines later... dispatch_sync(myQueue, ^{ [self doStuff2]; }); The first dispatch is async. So, it will be done concurrently, right? How can that be if myQueue is serial? How can a serial queue do things in parallel or, if you will, out of order? thanks bbum dispatch

Number of threads created by GCD?

强颜欢笑 提交于 2019-11-28 04:32:46
Is there any good documention on how many threads are created by GCD? At WWDC, they told us it's modeled around CPU cores. However, if I call this example: for (int i=1; i<30000; i++) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [NSThread sleepForTimeInterval:100000]; }); } it opens 66 threads, even on an iPad1. (It also opens 66 threads when called on Lion natively). Why 66? First, 66 == 64 (the maximum GCD thread pool size) + the main thread + some other random non-GCD thread. Second, GCD is not magic. It is optimized for keeping the CPU busy with code

How to write into an array from a dispatch_apply (GCD) loop?

旧城冷巷雨未停 提交于 2019-11-28 03:50:55
问题 I have written code to calculate the dynamics of a large set of coupled master equation using the Runge-Kutta-method. The code contains a lot of for-loops, where each step is independent. I intend to use Grand Central Dispatch to speed up the program. I based my attempt on an example I found at http://www.macresearch.org/cocoa-scientists-xxxi-all-aboard-grand-central . Neither my code nor the example on macresearch compile on my machine (MacOSX 10.6.8 Xcode 4.0.2). So here is my code: ...

Which tasks are more suitable to NSOperation than GCD? [duplicate]

醉酒当歌 提交于 2019-11-28 03:50:17
This question already has an answer here: NSOperation vs Grand Central Dispatch 8 answers Which tasks would be better suited to using NSOperation as opposed to using GCD when programming for the iPhone? To me they seem to do the same thing. I can't see the strengths and weaknesses one has over the other. NSOperation is built on top of GCD, so the question is more about whether you use NSOperation or pass a block directly to GCD. An NSOperation is bulky and needs more boiler-plate codes to set it up, but it has a lot more functionality. You can create the same NSOperation subclass in various

Wait for async task to finish completion block before returning in app delegate

痴心易碎 提交于 2019-11-28 03:28:42
I'm using a subclass of UIManagedDocument to use Core Data in my project. The point is for the subclass to return a singleton instance so that my screens can simply call it and the managed object context remains the same for all of them. Before using the UIManagedDocument , I need to prepare it by opening it if its file path already exists, or creating it if it doesn't yet. I created a convenience method prepareWithCompletionHandler: in the subclass to facilitate both scenarios. @implementation SPRManagedDocument // Singleton class method here. Then... - (void)prepareWithCompletionHandler: