grand-central-dispatch

How do I create a deadlock in Grand Central Dispatch?

喜夏-厌秋 提交于 2019-11-30 06:33:54
In Apple docs, it says: Important: You should never call the dispatch_sync or dispatch_sync_f function from a task that is executing in the same queue that you are planning to pass to the function. This is particularly important for serial queues, which are guaranteed to deadlock, but should also be avoided for concurrent queues. How do you write the code to do exactly this? An intentional deadlock on a certain queue: dispatch_queue_t queue = dispatch_queue_create("my.label", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ dispatch_sync(queue, ^{ // outer block is waiting for this inner block

How to modify the priority of a custom GCD queue?

不羁岁月 提交于 2019-11-30 06:27:04
I've created a GCD queue like this: dispatch_queue_t q = dispatch_queue_create("com.testcompany.myqueue", NULL); When I dispatch tasks to that queue, it is way slower than simply executing the task on the main thread. dispatch_async(q, ^(void) { [self performHeavyCalculationAndUpdateUI]; }); My suspicion is that the queue has a very low priority by default. How can I change the priority of this queue? Or is there something else I must do? If you're doing UIKit stuff, in your block that's running on the secondary queue, dispatch the UI updates back to the main queue from within the secondary

Use of the terms “queues”, “multicore”, and “threads” in Grand Central Dispatch

≯℡__Kan透↙ 提交于 2019-11-30 06:20:43
问题 I am trying to get my head around the concepts of Grand Central Dispatch. I want to understand these quotes from Vandad's book on Concurrent Programming. The real use for GCD is to dispatch tasks to multiple cores , without making you the programmer, worry about which core is executing which task. and At the heart of GCD are dispatch queues. Dispatch queues are pools of threads . and finally You will not be working with these threads directly. You will just work with dispatch queues,

What happens to Dispatch Queues when UIViewController is Deallocated?

旧街凉风 提交于 2019-11-30 05:26:31
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 self.sessionQueue captures self. Is this a reference cycle? self is not referencing the closure, just the

Dispatch group - cannot notify to main thread

混江龙づ霸主 提交于 2019-11-30 04:58:38
After reading Swift 3 evolution on GCD, I am trying to create dispatch group. The problem is the group.notify(queue: do not notify when I pass DispatchQueue.main as a queue, although it does work for background queue. Also I am not sure my syntax is all correct, as I am trying to convert code from Swift 2 to Swift 3. typealias CallBack = (result: Bool) -> Void func longCalculations (completion: CallBack) { let backgroundQ = DispatchQueue.global(attributes: .qosBackground) let group = DispatchGroup() var fill:[Int] = [] for item in 0...200 { group.enter() if item > 50 { fill.append(item) }

Applying Effect to iPhone Camera Preview “Video”

不羁的心 提交于 2019-11-30 04:34:25
My goal is to write a custom camera view controller that: Can take photos in all four interface orientations with both the back and, when available, front camera. Properly rotates and scales the preview "video" as well as the full resolution photo. Allows a (simple) effect to be applied to BOTH the preview "video" and full resolution photo. Implementation (on iOS 4.2 / Xcode 3.2.5): Due to requirement (3), I needed to drop down to AVFoundation. I started with Technical Q&A QA1702 and made these changes: Changed the sessionPreset to AVCaptureSessionPresetPhoto. Added an

Why NSOperationQueue is faster than GCD or performSelectorOnMainThread when they process a large number of tasks on the main Thread?

﹥>﹥吖頭↗ 提交于 2019-11-30 04:07:10
for example, i have 100 times the for loop. and need to update UIImageView,and the last 2 method is same slowly. why? what is the different between they? //fastest [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [btnThumb setImage:[UIImage imageWithData:data] forState:UIControlStateNormal]; [scrollView addSubview:btnThumb]; }]; //slowly dispatch_async(dispatch_get_main_queue(), ^ { [btnThumb setImage:[UIImage imageWithData:data] forState:UIControlStateNormal]; [scrollView addSubview:btnThumb]; }); //slowly [btnThumb setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];

Does NSURLSession Take place in a separate thread?

自作多情 提交于 2019-11-30 03:21:42
问题 I was designing an app that uses NSURLSession and thinking about putting it in a different thread with Grand Central Dispatch, but if NSURLSession automatically does that in the background, I wouldn't have to use GCD then, correct? So in other words, does NSURLSession automatically use Grand Central Dispatch in the background, so we don't have to worry about it? 回答1: Yes, NSURLSession does its work in a background thread. The download ALWAYS takes place on a background thread. EDIT: There is

Asynchronous url requests inside dispatch_async

女生的网名这么多〃 提交于 2019-11-30 02:29:40
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)myAsyncMultipleURLRequestFunction { for (int i=0; i<count; i++) { NSURLConnection *loginConnection = [[NSURLConnection alloc]

Get current dispatch queue?

冷暖自知 提交于 2019-11-30 01:46:09
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 it's 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? Michael Dautermann You do have the option of " dispatch_get_current_queue() " , however the iOS 6.1 SDK defines this API with these disclaimers: " Recommended for debugging and logging purposes only: " and " This function is