grand-central-dispatch

Keep blocks inside a dictionary

吃可爱长大的小学妹 提交于 2019-11-28 20:42:34
I have my own method that takes a block as an argument. I want to keep track of that block inside an NSDictionary. What is the best way to add the block to the dictionary? I tried this code but after executing the line below (setObject...) the dictionary is still empty. I presume that is because the block is not of type NSObject. But what is the right way to do this? - (void)startSomething:(NSURLRequest*)request block:(void (^)(NSURLResponse*, NSData*, NSError*))handler { NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [pendingRequests setObject

Have you noticed that dispatch_after runs ~10% too slow on iOS devices?

无人久伴 提交于 2019-11-28 20:22:10
问题 Lately I've been using dispatch_after instead of performSelector:withObject:afterDelay when I want to trigger some code after a delay. The code is cleaner, it has access to the enclosing scope, I can put the code in-line instead of writing a throw-away method, etc, etc. My code might look like this: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ //Delayed-execution code goes here. } ); However, I recently discovered that the time-to

Order of operations in runloop on iOS

落花浮王杯 提交于 2019-11-28 19:00:26
What is the order of operations on iOS? I'm thinking sepcifically about timing of setNeedsLayout and layoutSubviews setNeedsDisplay and drawRect touch recognition [NSTimer scheduledTimerWithTimeInterval:0.000001 tar(...)] dispatch_async(dispatch_get_main_queue(), ^{ /* code */} As an example of an answer I would like to receive it could be in this format: dispatch_async on main Happens before the next runcycle drawRect Happens at the end of the runcycle rob mayoff (Parts of this are copied from my answer to a similar question .) It turns out that the run loop is complicated, and a simple

Operation Queue vs Dispatch Queue for iOS Application

五迷三道 提交于 2019-11-28 18:32:57
What are the differences between Operation Queue and Dispatch Queue? Under what circumstances will it be more appropriate to use each? 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 dependencies between individual operations for example, which isn't possible with plain GCD queues. It is also

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

你。 提交于 2019-11-28 17:36:08
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, dispatching tasks to these queues and asking queues to invoke your task. I have bolded the key terms. Are

Waiting for multiple asynchronous download tasks

只谈情不闲聊 提交于 2019-11-28 17:33:37
I want to download some files, for example 100 files, at the same time. So I decided to add my download threads to a dispatch queue, and GCD will adjust how many threads will run at the same time. The problem here is: the block in dispatch_async will be completed immediately, because task will run on another thread. So, if urls 's length is 100, it will create 100 threads immediately. var queueDownloadTask = dispatch_queue_create("downloadQueue", nil) for url in urls { dispatch_async(queueDownloadTask) { let config = NSURLSessionConfiguration.defaultSessionConfiguration() let

Grand Central Dispatch vs. NSThread

安稳与你 提交于 2019-11-28 17:17:46
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",(int)i); }); } And the results: 2011-04-13 19:41:07.806 GDC[1494:5e03] Thread#0 2011-04-13 19:41:07

Objective C — What is the fastest and most efficient way to enumerate an array?

旧时模样 提交于 2019-11-28 17:10:31
问题 Edit I read through some articles on blocks and fast enumeration and GCD and the like. @Bbum, who's written many articles on the subject of GCD and blocks, says that the block enumeration methods are always as fast or faster than the fast enumeration equivalents. You can read his reasoning here. While this has been a fascinating, intellectual conversation, I agree with those who said that it really depends on the task at hand. I have some tasks to accomplish and I need them done fast, cheap,

What are the different ways for calling my method on separate thread?

懵懂的女人 提交于 2019-11-28 17:03:46
问题 I have some data calculation method (let it be "myMethod:"), and I want to move the call to another thread because I don't want to block my main UI functionality. So, started to do some research on how to call my method on another thread. As far as I see, currently, there are a lot of different ways for doing that. Here's a list: a) using pure threads (available since iOS 2.0): [NSThread detachNewThreadSelector:@selector(myMethod:) toTarget:self withObject:_myParamsArray]; b) using a simple

NSInputStream stops running, sometimes throws EXC_BAD_ACCESS

情到浓时终转凉″ 提交于 2019-11-28 16:52:50
问题 (UPDATED) this is the problem in a nutshell: in iOS I want to read a large file, do some processing on it (in this particular case encode as Base64 string() and save to a temp file on the device. I set up an NSInputStream to read from a file, then in (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode I'm doing most of the work. For some reason, sometimes I can see the NSInputStream just stops working. I know because I have a line NSLog(@"stream %@ got event %x", stream,