grand-central-dispatch

Grand Central Strategy for Opening Multiple Files

瘦欲@ 提交于 2019-11-29 10:15:38
问题 I have a working implementation using Grand Central dispatch queues that (1) opens a file and computes an OpenSSL DSA hash on "queue1", (2) writing out the hash to a new "side car" file for later verification on "queue2". I would like to open multiple files at the same time, but based on some logic that doesn't "choke" the OS by having 100s of files open and exceeding the hard drive's sustainable output. Photo browsing applications such as iPhoto or Aperture seem to open multiple files and

“Pausing” the Game in Swift

巧了我就是萌 提交于 2019-11-29 08:48:06
I created a game in Swift that involves monsters appearing. Monsters appear, and disappear, based on timers using something like this: func RunAfterDelay(_ delay: TimeInterval, block: @escaping ()->()) { let time = DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) DispatchQueue.main.asyncAfter(deadline: time, execute: block) } and then I'd just call it like this (for example to spawn after 2 seconds): ///Spawn Monster RunAfterDelay(2) { [unowned self] in self.spawnMonster() } I then do something similar for hiding (after x seconds, I despawn the monster).

checking for equality on dispatch_queue_t

百般思念 提交于 2019-11-29 08:23:40
How can I check for equality between dispatch_queue_t vars? dispatch_queue_t currentQueue = dispatch_get_current_queue(); dispatch_queue_t mainQueue = dispatch_get_main_queue(); if (currentQueue == mainQueue) { } from the docs: typedef struct dispatch_queue_s *dispatch_queue_t; I'm not sure but does this mean that it's a pointer to a dispatch_queue_s struct? Since I can't check equality on pointers, I'm not sure how can I check if a dispatch_queue_t is the same as another? This depends on the queue you're on. In this particular case use: if ([NSThread isMainThread]) {} In general, you can use

What GCD queue, main or not, am I running on?

混江龙づ霸主 提交于 2019-11-29 08:20:26
问题 I am trying to write some thread safe methods so I am using: ... dispatch_queue_t main = dispatch_get_main_queue(); dispatch_sync(main,^{ [self doSomethingInTheForeground]; }); ... But If I am on the main thread that is not necessary, and I can skip all those dispatch calls, so I would like to know what thread I currently am on. How can I know this? Or, perhaps it does not make difference (in performance) doing it? Is it ok to do this comparison? if (dispatch_get_main_queue() == dispatch_get

Is there any way to implement dispatch_cancel()?

半世苍凉 提交于 2019-11-29 07:54:20
So far I have gone through the doc of GCD, however it seems there misses dispatch_cancel() which I want to use it to cancel all dispatch's blocks invocation. Is there any way to implement dispatch_cancel()? As @HampusNilsson mentions, you can't reasonably cancel any in-flight operation in a non-garbage collected environment (such as this) because it would inherently leak resources and leave the process in an indeterminate state. NSOperationQueue has a cancellation API, and that API can be used to implement cancellation of in-flight operations, provided that the operations themselves are

Can I limit concurrent requests using GCD?

荒凉一梦 提交于 2019-11-29 07:12:51
I'm acquiring data from a database asynchronously. Is there any way I can limit the concurrent requests to a number, but still execute the rest? I saw a post using NSOperation and NSOperationQueue but this is too complicated for me. Is there any other ways to do it? Can GCD achieve it? Something like this: ... //only make one of these obviously :) remaking it each time you dispatch_async wouldn't limit anything dispatch_semaphore_t concurrencyLimitingSemaphore = dispatch_semaphore_create(limit); ... //do this part once per task, for example in a loop dispatch_semaphore_wait

Performance test: sem_t v.s. dispatch_semaphore_t and pthread_once_t v.s. dispatch_once_t

二次信任 提交于 2019-11-29 06:56:02
问题 I wanted to know what would be better/faster to use POSIX calls like pthread_once() and sem_wait() or the dispatch_* functions, so I created a little test and am surprised at the results (questions and results are at the end). In the test code I am using mach_absolute_time() to time the calls. I really don’t care that this is not exactly matching up with nano-seconds; I am comparing the values with each other so the exact time units don't matter, only the differences between the interval do.

How to modify the priority of a custom GCD queue?

只愿长相守 提交于 2019-11-29 06:12:14
问题 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? 回答1: If you're doing UIKit stuff, in your block that's

method not called with dispatch_async and repeating NSTimer

笑着哭i 提交于 2019-11-29 04:27:45
I am developing an app where i want to call method in separate queue using dispatch_async . I want to call that method repeatedly after certain interval of time. But the method is not getting called. I don't know whats wrong. Here is my code: dispatch_async( NotificationQueue, ^{ NSLog(@"inside queue"); timer = [NSTimer scheduledTimerWithTimeInterval: 20.0 target: self selector: @selector(gettingNotification) userInfo: nil repeats: YES]; dispatch_async( dispatch_get_main_queue(), ^{ // Add code here to update the UI/send notifications based on the // results of the background processing }); })

File monitoring using Grand Central Dispatch

我与影子孤独终老i 提交于 2019-11-29 04:21:05
I'm using the code example by David Hamrick to monitor a file using GCD. int fildes = open("/path/to/config.plist", O_RDONLY); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fildes, DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE, queue); dispatch_source_set_event_handler(source, ^ { //Reload the config file }); dispatch_source_set_cancel_handler(source, ^ { /