grand-central-dispatch

OperationQueue.main vs DispatchQueue.main

女生的网名这么多〃 提交于 2019-12-03 08:27:28
问题 When you need to perform something on the main thread in the completion block of a networking task or an operation, which of these ways to get it would be the most appropriate and why?: OperationQueue.main.addOperation DispatchQueue.main.async 回答1: For details on the differences between the two types of queue, see Lion's answer. Both approaches will work. However, NSOperation is mostly needed when more advanced scheduling (including dependencies , canceling , etc.) is required. So in this

How do I watch for file changes on OS X?

半世苍凉 提交于 2019-12-03 08:24:53
I would like to be notified of writes into a given file – without polling, without having to read from the file and without having to monitor the parent directory and watch the file modification time stamps. How do I do that? I couldn’t find a simple example, so I’m contributing what I came up with for future reference: @interface FileWatch () @property(assign) dispatch_source_t source; @end @implementation FileWatch @synthesize source; - (id) initWithPath: (NSString*) path targetQueue: (dispatch_queue_t) queue block: (dispatch_block_t) handler { self = [super init]; int descriptor = open(

Could Grand Central Dispatch (`libdispatch`) ever be made available on Windows?

Deadly 提交于 2019-12-03 08:03:47
问题 I’m looking into multithreading, and GCD seems like a much better option than manually writing a solution using pthread.h and pthreads-win32 . However, although it looks like libdispatch is either working on, or soon going to be working on, most newer POSIX-compatible systems… I have to ask, what about Windows? What are the chances of libdispatch being ported to Windows? What are the barriers preventing that from happening? If it came down to it, what would I need to do to preform that

In a UITableView, best method to cancel GCD operations for cells that have gone off screen?

天大地大妈咪最大 提交于 2019-12-03 07:53:18
I have a UITableView that loads images from a URL into cells asynchronously using GCD. Problem is if a user flicks past 150 rows, 150 operations queue up and execute. What I want is to dequeue/cancel the ones that blew past and went off screen. How do I do this? My code at this point (pretty standard): - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath // after getting the cell... dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (runQ) { NSString *galleryTinyImageUrl = [[self.smapi getImageUrls:imageId

GCD serial queue does not seem to execute serially

只谈情不闲聊 提交于 2019-12-03 07:33:53
I have a method that at times can be invoked throughout my code. Below is a very basic example, as the code processes images and files off of the iphone photo gallery and marks them already processed when done with the method. @property (nonatomic, assign) dispatch_queue_t serialQueue; .... -(void)processImages { dispatch_async(self.serialQueue, ^{ //block to process images NSLog(@"In processImages"); .... NSLog(@"Done with processImages"); }); } I would think that each time this method is called I would get the below output... "In processImages" "Done with processImages" "In processImages"

Apple doc's GCD Producer-Consumer solution wrong?

拥有回忆 提交于 2019-12-03 07:08:09
问题 In the Migrating Away from Threads section of Apple's Concurrency Programming Guide, there is Changing Producer-Consumer Implementations, which claims that the typical multistep pthread mutex + condition variable implementation can be simplified using GCD. With dispatch queues, you can simplify the producer and consumer implementations into a single call: dispatch_async(queue, ^{ // Process a work item. }); When your producer has work to be done, all it has to do is add that work to a queue

Easy example of Grand Central Dispatch

左心房为你撑大大i 提交于 2019-12-03 06:59:49
问题 I'm newbie programming for mac and i'm really surprised on Grand Central Dispatch. I read about that and looks like the perfect solution for parallel programming. I worked with POSIX threads and want to move to GCD. I saw the samples codes in the Apple Developer Connection, but It confused me so much. I searched for an easy example with two threads to start but i can't find it. How can I do this sample code using GCD ??? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /*

Is it safe to schedule and invalidate NSTimers on a GCD serial queue?

我怕爱的太早我们不能终老 提交于 2019-12-03 06:33:06
What's the right way to do this? The NSTimer documentation says this: Special Considerations You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly. Since GCD doesn't assure you that a serial queue will always run blocks on the same thread, what's the right way to ensure that you schedule and invalidate an NSTimer on the same thread? EDIT: Following the advise of the answer below, I created

comparison GCD vs. performSelectorInBackground: dispatch_async not in background

独自空忆成欢 提交于 2019-12-03 04:39:04
问题 Grand Central Dispatch is great and reduces the amount of code but why I cannot run something on a background thread? I have made a sample application to show what I mean (none of the commented work): - (IBAction)performSel { [self performSelectorInBackground:@selector(doStuff) withObject:nil]; [NSThread sleepForTimeInterval:3]; [[self.view.subviews lastObject] removeFromSuperview]; } - (IBAction)gcd { dispatch_async(dispatch_queue_create("myGCDqueue", NULL), ^(void) { //dispatch_sync

How to dispatch code blocks to the same thread in iOS?

亡梦爱人 提交于 2019-12-03 04:25:24
问题 Main aspect of the question: It's about iOS. Can I somehow dispatch code blocks in a way, that they will all (a) run in background and (b) on the same thread? I want to run some time-consuming operations in background, but these have to be run on the same thread, because they involve resources, that mustn't be shared among threads. Further technical details, if required: It's about implementing an sqlite plugin for Apache Cordova, a framework for HTML5 apps on mobile platforms. This plugin