grand-central-dispatch

NSURLConnection and grand central dispatch

假如想象 提交于 2019-11-27 02:38:28
Is it advisable to wrap up NSUrlConnection in a gcd style blocks and run it on a low_priority queue? I need to ensure that my connections are not happening on the main thread and the connections need to be asynchronous. I also need several simultaneous requests to go at once. If I go the gcd route, I'm not sure which thread the NSUrlConnectionDelegate methods get invoked on. NSURLConnection relies on delegates so once the connection is complete, whatever wrapper class that handles it will need to invoke its caller. I'm not certain how to deal with all of the various callbacks that are invoked

Core Data and threads / Grand Central Dispatch

ⅰ亾dé卋堺 提交于 2019-11-27 02:36:57
I'm a beginner with Grand Central Dispatch (GCD) and Core Data, and I need your help to use Core Data with CGD, so that the UI is not locked while I add 40.000 records to Core Data. I know that CD is not thread safe, so I have to use another context, and then save the data and merge contexts, as far as I was able to understand from some articles. What I couldn't do yet, is put the pieces together. So, in my code, I need your help on how to to that. I have: /*some other code*/ for (NSDictionary *memberData in arrayWithResult) { //get the Activities for this member NSArray

Create a high priority serial dispatch queue with GCD

妖精的绣舞 提交于 2019-11-27 02:08:27
问题 How can I create a custom serial queue that runs at high priority? Right now I'm using myQueue = dispatch_queue_create("com.MyApp.MyQueue", NULL); but this doesn't seem to allow for setting a priority? 回答1: Create a serial queue, then use dispatch_set_target_queue() to set its target queue to the high priority queue. Here's how: dispatch_set_target_queue(myQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)); Now myQueue should run serially with high priority. Here's another SO

Adding items to Swift array across multiple threads causing issues (because arrays aren't thread safe) - how do I get around that?

好久不见. 提交于 2019-11-27 01:58:51
I want to add given blocks to an array, and then run all the blocks contained in the array, when requested. I have code similar to this: class MyArrayBlockClass { private var blocksArray: Array<() -> Void> = Array() private let blocksQueue: NSOperationQueue() func addBlockToArray(block: () -> Void) { self.blocksArray.append(block) } func runBlocksInArray() { for block in self.blocksArray { let operation = NSBlockOperation(block: block) self.blocksQueue.addOperation(operation) } self.blocksQueue.removeAll(keepCapacity: false) } } The problem comes with the fact that addBlockToArray can be

dispatch_async and calling a completion handler on the original queue

流过昼夜 提交于 2019-11-27 01:11:41
问题 I've seen some related questions but none seem to answer this case. I want to write a method that will do some work in the background. I need this method to call a completion callback on the same thread / queue used for the original method call. - (void)someMethod:(void (^)(BOOL result))completionHandler { dispatch_queue_t current_queue = // ??? // some setup code here dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ BOOL ok = // some result // do some long

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

爷,独闯天下 提交于 2019-11-27 00:59:51
问题 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? 回答1: 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

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

拥有回忆 提交于 2019-11-27 00:45:19
问题 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

How to stop/cancel/suspend/resume tasks on GCD queue

依然范特西╮ 提交于 2019-11-27 00:28:09
问题 How to stop/cancel/suspend/resume tasks on GCD queue How does one stop background queue operations? I want to stop some screens in our app. And some screens it should be auto resume. So, how does one pass a queue in iOS? I mean when user have browsing the app time we run the background thread in dispatch_queue_t. But it never stops and resume in the code. So how does one suspend and resume a queue 回答1: To suspend a dispatch queue, it's simply dispatch_suspend(queue) in Objective-C or Swift 2

Number of threads created by GCD?

北慕城南 提交于 2019-11-27 00:22:53
问题 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? 回答1: First, 66 == 64 (the maximum GCD thread pool size) + the main thread + some

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

拟墨画扇 提交于 2019-11-27 00:15:50
问题 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. 回答1: 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