grand-central-dispatch

Why is this dispatch_sync() call freezing?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 11:35:13
I'm using the Kiwi testing framework to test an authentication method in my app. The test freezes at a call to dispatch_sync which looks like this: dispatch_queue_t main = dispatch_get_main_queue(); dispatch_sync(main, ^ { [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAuthenticationSuccess object:nil userInfo:ret]; }); I'd like to know why it freezes there, if anyone has any hints. For the second part of your question regarding the hint on the freeze: When calling dispatch_sync on a queue, always verify that this queue is not already the current queue ( dispatch_get

Operation Queue vs Dispatch Queue for iOS Application

折月煮酒 提交于 2019-11-27 11:26:00
问题 What are the differences between Operation Queue and Dispatch Queue? Under what circumstances will it be more appropriate to use each? 回答1: NSOperationQueue predates Grand Central Dispatch and on iOS it doesn't use GCD to execute operations (this is different on Mac OS X). It uses regular background threads which have a little more overhead than GCD dispatch queues. On the other hand, NSOperationQueue gives you a lot more control over how your operations are executed. You can define

dispatch_get_global_queue vs dispatch_get_main_queue

岁酱吖の 提交于 2019-11-27 10:39:58
Starting to learn about core data and dispatch_async. There is a block of code to get url of image from set of data and set it to model of core data like below dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSString *urlString = [[[photoDictionary valueForKey:@"images"] objectAtIndex:0] valueForKey:@"url"]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; dispatch_async(dispatch_get_main_queue(), ^{ [photoModel setValue:imageData forKey:@"photoImageData"]; Can somebody explain to me why we use dispatch_get_global_queue for the

Grand Central Dispatch vs. NSThread

筅森魡賤 提交于 2019-11-27 10:22:53
问题 I created some test code for NSThread and Grand Central Dispatch (GCD): - (void)doIt:(NSNumber *)i { sleep(1); NSLog(@"Thread#%i", [i intValue]); } - (IBAction)doWork:(id)sender { for (int i = 0; 10 > i; i++) { NSNumber *t = [NSNumber numberWithInt:i]; [NSThread detachNewThreadSelector:@selector(doIt:) toTarget:self withObject:t]; } sleep(1); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply(10, queue, ^(size_t i) { sleep(1); NSLog(@"GCD#%u

How do you schedule a block to run on the next run loop iteration?

别等时光非礼了梦想. 提交于 2019-11-27 10:16:46
I want to be able to execute a block on the next run loop iteration. It's not so important whether it gets executed at the beginning or the end of the next run loop, just that execution is deferred until all code in the current run loop has finished executing. I know the following doesn't work because it gets interleaved with the main run loop so my code might execute on the next run loop but it might not. dispatch_async(dispatch_get_main_queue(),^{ //my code }); The following I believe suffers the same problem as above: dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){ /

Best practice to send a lot of data in background on iOS4 device?

一个人想着一个人 提交于 2019-11-27 09:53:12
问题 I have an app that needs to send data (using POST) to a server. This function has to be on one of the NavigationController sub-controllers and user should be able to navigate away from this controller and/or close the app (only iPhone4/iOS4 will be supported). Should I use threads/NSOperations or/and send data using existing asynchronous methods? Any ideas/best practices how to implement this? 回答1: OK, I'll answer my own question. First, like tc said, it's better to have this call on the

(iOS) dispatch_async() vs. NSOperationQueue

限于喜欢 提交于 2019-11-27 09:14:42
问题 I learned iOS programming thanks to Stanford's CS193p course (on iTunes U) as well as the iOS programming book from Big Nerd Ranch. In both of those, they recommend using dispatch_async() , dispatch_get_main_queue() , etc. to handle threading and concurrent operations. However, at WWDC 2012's session on building concurrent UI, the speaker recommended the use of NSOperationQueue . What are the differences between dispatch_*() and NSOperationQueue , and is there any reason (technical,

Call completion block when two other completion blocks have been called

流过昼夜 提交于 2019-11-27 08:19:34
问题 I have a function doEverything that takes a completion block. It calls two other functions, doAlpha and doBeta which both have completion blocks. These two functions should run asynchronously. I want to call doEverything 's completion block after both of the other functions have called their completion blocks. Currently, it looks like this: func doEverything(completion: @escaping (success) -> ())) { var alphaSuccess = false var betaSuccess = false doAlpha { success in alphaSuccess = success }

How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

本秂侑毒 提交于 2019-11-27 07:24:15
Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function could be called from something already running on that queue, so I needed to check for this case in order to prevent a deadlock from a synchronous dispatch to the same queue. I used code like the following to do this: void runSynchronouslyOnVideoProcessingQueue(void (^block)(void)) { dispatch_queue_t videoProcessingQueue = [GPUImageOpenGLESContext sharedOpenGLESQueue]; if (dispatch_get_current

Why does it take such a long time for UI to be updated from background thread?

被刻印的时光 ゝ 提交于 2019-11-27 06:28:05
问题 I understand that all UI updates must be done from Main thread. But purely for the sake of deeper understanding how GCD and dispatch main work: I have a button that runs a network call and in its completionHandler I eventually do: self.layer.borderColor = UIColor(red: 255/255.0, green: 59/255.0, blue: 48/255.0, alpha: 1.0).cgColor self.layer.borderWidth = 3.0 For the color change to happen it takes 6-7 seconds. Obviously if run the above code from main thread it would change the border color