grand-central-dispatch

iOS stop global queue from running

旧城冷巷雨未停 提交于 2019-12-08 05:16:13
问题 Code: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [ServerAPI API_GetChatList:self withUserId:[self getUserIDFromUserDefaults] withScheduleId:strGroupId withType:@"group"]; dispatch_async(dispatch_get_main_queue(), ^{ [self performSelector:@selector(getChatList) withObject:nil afterDelay:10]; }); }); I used dispatch global queue to call a method for every 10 seconds.It is working fine.The problem is global queue is keep running in other controllers too

Geocoding Multiple Locations - Knowing When “All” Completion Blocks Have Been Called

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 02:53:45
问题 I am using the CoreLocation's geocoder to get the CLLocation coordinates for multiple map items. The geocoder calls a completion block on completion for each item. How do I create a similar block functionality which is called when all of these containing asynchronous geocoder calls have been completed? (I could use a manual counter. But there must be a more elegant solution) Here's my geocoding function so far. It loops through an array of location items and starts a new geocoding process for

GCD - Critical Section/Mutex

Deadly 提交于 2019-12-08 02:09:38
问题 Can somebody answer with short example: How to correctly Lock code part with condition: if this part is locked by some thread don't hold other threads just skip this part by other threads and keep going. 回答1: ok, here is the working example (credit goes to @KenThomases ...) import Dispatch let semaphore = DispatchSemaphore(value: 1) let printQueue = DispatchQueue(label: "print queue") let group = DispatchGroup() func longRuningTask(i: Int) { printQueue.async(group: group) { print(i,"GREEN

Why NSURLConnection delegate methods don't get called, when using the global dispatch queue?

老子叫甜甜 提交于 2019-12-07 17:22:11
问题 When I do the following: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL), ^{ create NSURLRequest; create NSURLConnectionDelegate; create NSURLConnection; start NSURLConnection; }); The delegate's methods never get called. But when I do dispatch_async(dispatch_get_main_queue(), ^{ create NSURLRequest; create NSURLConnectionDelegate; create NSURLConnection; start NSURLConnection; }); They do get called. Why? UPD http://developer.apple.com/library/ios/#qa/qa1712/

How do I repeatedly loop dispatch_after statements?

一世执手 提交于 2019-12-07 17:02:09
问题 I want to have a for loop with dispatch_after statements in them. Problem is that the dispatch_after calls don't seem to go in line with the for loop. In other words, I want it to only start the next iteration of the for loop after the statements in the dispatch_after block have executed. How would I do this? Use Case I want to present words on screen. Traditionally I show one word per second. But depending on the word length, I now want to show longer words for slightly longer, and shorter

dispatch_queue_t SerialQueue how check if is still running. ios GCD

∥☆過路亽.° 提交于 2019-12-07 16:23:27
I need to do a validation and for this I need to know if the task is still running i use this code for launch the task ` SerialQueue = dispatch_queue_create("miColaEnSerie", NULL); dispatch_async(SerialQueue, ^{ [self loadImageFriend:init finalWhitNumber:final img1WithArray:infoImages1 img2WithArray:infoImages2 img3WithArray:infoImages3]; });` If you have only one task at a time in your SerialQueue then you can add atomic property to a class like: @property (assign) BOOL isSerialQueueRunning; Then: isSerialQueueRunning = YES; dispatch_async(SerialQueue, ^{ [self loadImageFriend:init

Does dispatch_after block the main thread?

穿精又带淫゛_ 提交于 2019-12-07 14:26:08
问题 I'm setting a timer so that after a second passes I reset a value for my keyboard extension. The problem is that I feel like the following call is stalling my UI: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self resetDoubleTapBool]; }) Is there an asynchronous way of doing this, or a better way in general? Thanks! 回答1: The dispatch_after() call itself does not block. At (or shortly after) the appointed time, the block will be submitted to

What is the purpose of Semaphore.wait(timeout: .now())?

元气小坏坏 提交于 2019-12-07 13:40:15
问题 Looking at some Apple code sample, I found this: func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { // wait() is used to drop new notifications if old ones are still processing, to avoid queueing up a bunch of stale data. if metadataObjectsOverlayLayersDrawingSemaphore.wait(timeout: .now()) == .success { DispatchQueue.main.async { // Some processing... self.metadataObjectsOverlayLayersDrawingSemaphore

calling performSelectorInBackground: from background thread

社会主义新天地 提交于 2019-12-07 12:19:48
问题 What is the real effect of calling performSelectorInBackground:... from a method that is running in the background? I want it to run asynchronously For example: - (void) _imageBufferWasUpdated{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //do something here if(shouldContinue){ [self performSelectorInBackground:@selector(_loop) withObject:nil]; } [pool release]; } _imageBufferWasUpdated will run in the background and I want to call _loop method asynchronously (in the background

dispatch_async in NSManagedObjectContext's performBlock

女生的网名这么多〃 提交于 2019-12-07 11:54:01
问题 According to the WWDC 2012 video, "Core Data Best Practices", dispatch_sync should be used to run some kind of callback in performBlock of context, which is created as a type of NSPrivateQueueConcurrencyType . Why is that? Can I use dispatch_async(dispatch_get_main_queue(), 0) ... to call some UI-related callbacks in private queue's context's performBlock ? 回答1: No. NSPrivateQueueConcurrencyType manages it's own internal queue, and does not enjoy you trying to leave one of it's threads to do