grand-central-dispatch

Best practice to send a lot of data in background on iOS4 device?

对着背影说爱祢 提交于 2019-11-28 16:44:08
I have an app that needs to send data (using POST) to a server. This function has to be on one of the NavigationController sub-controllers and user should be able to navigate away from this controller and/or close the app (only iPhone4/iOS4 will be supported). Should I use threads/NSOperations or/and send data using existing asynchronous methods? Any ideas/best practices how to implement this? OK, I'll answer my own question. First, like tc said, it's better to have this call on the application delegate, so that the View in the NavigationController can be closed. Second, mark beginning of the

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

流过昼夜 提交于 2019-11-28 16:18:52
问题 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

Data corruption when reading realtime H.264 output from AVAssetWriter

拟墨画扇 提交于 2019-11-28 15:53:14
问题 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

What is the difference between dispatch_get_global_queue and dispatch_queue_create?

只愿长相守 提交于 2019-11-28 15:49:15
问题 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. 回答1: As the documentation describes, a global queue is good for concurrent tasks (i.e. you're going to dispatch

(iOS) dispatch_async() vs. NSOperationQueue

我们两清 提交于 2019-11-28 15:28:27
I learned iOS programming thanks to Stanford's CS193p course (on iTunes U) as well as the iOS programming book from Big Nerd Ranch. In both of those, they recommend using dispatch_async() , dispatch_get_main_queue() , etc. to handle threading and concurrent operations. However, at WWDC 2012's session on building concurrent UI, the speaker recommended the use of NSOperationQueue . What are the differences between dispatch_*() and NSOperationQueue , and is there any reason (technical, performance, stylistic, or otherwise) that I should use one over the other? Is NSOperationQueue just an

Call completion block when two other completion blocks have been called

a 夏天 提交于 2019-11-28 14:00:02
I have a function doEverything that takes a completion block. It calls two other functions, doAlpha and doBeta which both have completion blocks. These two functions should run asynchronously. I want to call doEverything 's completion block after both of the other functions have called their completion blocks. Currently, it looks like this: func doEverything(completion: @escaping (success) -> ())) { var alphaSuccess = false var betaSuccess = false doAlpha { success in alphaSuccess = success } doBeta { success in betaSuccess = success } // We need to wait here completion(alphaSuccess &&

Stop a DispatchQueue that is running on the main thread

浪尽此生 提交于 2019-11-28 12:42:58
I have this block of code: DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay) { self.isShootingOnHold = false self.shoot() self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true) } Now, I want to be able to stop this thread from executing. How can I stop it from being executed? For instance, after 3 seconds, I decide I don't want that to execute anymore so I want to stop it. You can use DispatchWorkItem s. They

`[NSThread isMainThread]` always returns YES

陌路散爱 提交于 2019-11-28 12:01:23
This code dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSLog(@"Main Thread? %d", [NSThread isMainThread]); }); shows that I'm in the main thread. Even doing this: queue = dispatch_queue_create("nonMainQueue", NULL); still reports that I'm in the main queue. This is, it seems, because I'm using dispatch sync . Does this mean that my code is the same as not using dispatch_sync at all? Also: what's the point of dispatch_sync if it does nothing at all, then? Because queues are not threads, in order to check if you are on the main 'queue', you must use different code

App blocks while dipatching a queue

ぐ巨炮叔叔 提交于 2019-11-28 11:54:13
问题 Im using XMPPFramework and in it's code there's a method like this: - (NSDictionary *)occupants { if (dispatch_get_current_queue() == moduleQueue) { return occupants; } else { __block NSDictionary *result; dispatch_sync(moduleQueue, ^{//IT BLOCKS HERE, WITHOUT MESSAGE result = [occupants copy]; }); return [result autorelease]; } } [EDIT] It blocks inconsistently, not always, since the app is not doing anything I pause it and I see the thread has stopped there, and it never continues to

Why does it take such a long time for UI to be updated from background thread?

笑着哭i 提交于 2019-11-28 11:38:11
I understand that all UI updates must be done from Main thread. But purely for the sake of deeper understanding how GCD and dispatch main work: I have a button that runs a network call and in its completionHandler I eventually do: self.layer.borderColor = UIColor(red: 255/255.0, green: 59/255.0, blue: 48/255.0, alpha: 1.0).cgColor self.layer.borderWidth = 3.0 For the color change to happen it takes 6-7 seconds. Obviously if run the above code from main thread it would change the border color immediately. Question1 even though I don't have ANY other code to run, why doesn't the UI changes