grand-central-dispatch

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

浪子不回头ぞ 提交于 2019-11-29 23:05:30
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-excution from this code seems to run pretty consistently about 10% slower than requested. If I ask for a

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

让人想犯罪 __ 提交于 2019-11-29 21:32:52
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, and efficiently. Apple gives us many choices for how we want to enumerate an array, but I'm not sure

NSInputStream stops running, sometimes throws EXC_BAD_ACCESS

喜欢而已 提交于 2019-11-29 20:42:19
(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, (unsigned)eventCode); in the beginning of (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent

Create my own completion blocks in iOS

痴心易碎 提交于 2019-11-29 20:29:19
问题 I have an object which takes a long time to do some stuff (it downloads data from a server). How can I write my own completion block so that I can run... [downloader doSomeLongThing:^(void) { //do something when it is finished }]; I'm not sure how to save this block in the downloader object. 回答1: You can copy the block then invoke it: typedef void (^CallbackBlk)(); @property (copy) CallbackBlk cb; - (void)doSomething:(CallbackBlk)blk { self.cb = blk; // etc. } // when finished: self.cb(); 回答2

Perform on Next Run Loop: What's Wrong With GCD?

依然范特西╮ 提交于 2019-11-29 20:27:43
I'm trying these two approaches: dispatch_async(dispatch_get_main_queue(),^{ [self handleClickAsync]; }); and [self performSelector:@selector(handleClickAsync) withObject:nil afterDelay:0]; in response to a button press. The second allows the UIButton to highlight as one would expect and perform the handleClickAsync on the next run loop (I suppose: "sometime later" for sure). The first does not allow the UIButton instance to light up until the operation is completely done. What is the correct way to do this with GCD, or is performSelector still the only way? I believe the answer is found here

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

柔情痞子 提交于 2019-11-29 20:26:36
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 shortcut (available since iOS 2.0). Available from inherited NSObject but the method belongs to NSThread

Data corruption when reading realtime H.264 output from AVAssetWriter

大兔子大兔子 提交于 2019-11-29 20:02:09
I'm using some tricks to try to read the raw output of an AVAssetWriter while it is being written to disk. When I reassemble the individual files by concatenating them, the resulting file is the same exact number of bytes as the AVAssetWriter's output file. However, the reassembled file will not play in QuickTime or be parsed by FFmpeg because there is data corruption. A few bytes here and there have been changed, rendering the resulting file unusable. I assume this is occurring on the EOF boundary of each read, but it isn't consistent corruption. I plan to eventually use code similar to this

What is the difference between dispatch_get_global_queue and dispatch_queue_create?

丶灬走出姿态 提交于 2019-11-29 19:26:28
I'm writing a moderately complex iOS program that needs to have multiple threads for some of its longer operations (parsing, connections to the network...etc). However, I'm confused as to what the difference is between dispatch_get_global_queue and dispatch_queue_create . Which one should I use and could you give me a simple explanation of what the difference is in general? Thanks. As the documentation describes, a global queue is good for concurrent tasks (i.e. you're going to dispatch various tasks asynchronously and you're perfectly happy if they run concurrently) and if you don't want to

Speed up search using dispatch_async?

99封情书 提交于 2019-11-29 18:22:49
问题 I'm trying to speed up my app search , it get lags when there is a lot of data. so i'm trying to split search Predicate on UI by using dispatch_async not dispatch_sync cause no different if I use it. The problem is when i use dispatch_async , the app crash sometimes because [__NSArrayI objectAtIndex:]: index "17" beyond bounds . I now this happened because lets say the first one still work and reload the tableView and continue search will change the array size depend on result so in this case

Equivalent of GCD serial dispatch queue in iOS 3.x

左心房为你撑大大i 提交于 2019-11-29 17:47:22
问题 Apple's Grand Central Dispatch (GCD) is great, but only works on iOS 4.0 or greater. Apple's documentation says, "[A] serialized operation queue does not offer quite the same behavior as a serial dispatch queue in Grand Central Dispatch does" (because the queue is not FIFO, but order is determined by dependencies and priorities). What is the right way to achieve the same effect as GCD's serial dispatch queues while supporting OS versions before GCD was released? Or put another way, what is