grand-central-dispatch

iOS GCD Sync with Async Block

穿精又带淫゛_ 提交于 2019-11-30 18:10:50
问题 I have an async function with a block : [self performAsyncTaskCompletion:(void(^) () ) { //Do Something } ]; I need to call this function many times but in a synchronous way. I tried to use GCD queues : dispatch_queue_t queue = dispatch_queue_create("com.MyApp.task", NULL); for (int i = 0; i < array.count; i++) { dispatch_sync(queue, ^{ [self performAsyncTaskCompletion:(void(^) () ) { //Do Something } ]; }); } But it doesn't work because dispatch_sync is only waiting for the end of the block.

NSOperationQueue vs GCD

青春壹個敷衍的年華 提交于 2019-11-30 16:54:09
问题 In what cases would you prefer to use NSOperationQueue over GCD? From my limited experience of these two, I take it that with NSOperationQueue you basically have control over how many concurrent operations there are. With GCD you can't do this, since you are using a queue. Except you can somehow simulate this with a multi core processor, although still I think there's no way to control it. 回答1: NSOperationQueue is built on GCD as of iOS 4. Use the simplest API for the task at hand.Measure if

Downloading files in serial order using NSURLConnection in iOS

久未见 提交于 2019-11-30 15:50:29
问题 I want to download 3 files in serial order. Two of them are txt files and one is .gz file. I am using NSURLConnection to download the above files. I am very new to iOS Programming. I have seen in other question in SO and google that we can use serial dispatch queue to do some operation serially. But I don't know how to do this with NSURLConnection. I tried below but did not work. dispatch_queue_t serialQueue = dispatch_queue_create("com.clc.PropQueue", DISPATCH_QUEUE_SERIAL); dispatch_async

Downloading files in serial order using NSURLConnection in iOS

不问归期 提交于 2019-11-30 15:38:22
I want to download 3 files in serial order. Two of them are txt files and one is .gz file. I am using NSURLConnection to download the above files. I am very new to iOS Programming. I have seen in other question in SO and google that we can use serial dispatch queue to do some operation serially. But I don't know how to do this with NSURLConnection. I tried below but did not work. dispatch_queue_t serialQueue = dispatch_queue_create("com.clc.PropQueue", DISPATCH_QUEUE_SERIAL); dispatch_async(serialQueue, ^{ [self downloadProp]; }); dispatch_async(serialQueue, ^{ [self downloadDatabase]; });

Using Grand Central Dispatch in Swift to parallelize and speed up “for\" loops?

爷,独闯天下 提交于 2019-11-30 14:25:39
I am trying to wrap my head around how to use GCD to parallelize and speed up Monte Carlo simulations. Most/all simple examples are presented for Objective C and I really need a simple example for Swift since Swift is my first “real” programming language. The minimal working version of a monte carlo simulation in Swift would be something like this: import Foundation import Cocoa var winner = 0 var j = 0 var i = 0 var chance = 0 var points = 0 for j=1;j<1000001;++j{ var ability = 500 var player1points = 0 for i=1;i<1000;++i{ chance = Int(arc4random_uniform(1001)) if chance<(ability-points) {+

Create my own completion blocks in iOS

Deadly 提交于 2019-11-30 14:23:50
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. 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(); Since you're not using any parameters in your callback, you could just use a standard dispatch_block_t and

Why am I getting deadlock with dispatch_once?

柔情痞子 提交于 2019-11-30 13:26:52
问题 Why am I deadlocking? - (void)foo { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self foo]; }); // whatever... } I expect foo to be executed twice on first call. 回答1: Neither of the existing answers are quite accurate (one is dead wrong, the other is a bit misleading and misses some critical details). First, let's go right to the source: void dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func) { struct _dispatch_once_waiter_s * volatile *vval =

How do I create a deadlock in Grand Central Dispatch?

限于喜欢 提交于 2019-11-30 13:11:51
问题 In Apple docs, it says: Important: You should never call the dispatch_sync or dispatch_sync_f function from a task that is executing in the same queue that you are planning to pass to the function. This is particularly important for serial queues, which are guaranteed to deadlock, but should also be avoided for concurrent queues. How do you write the code to do exactly this? 回答1: An intentional deadlock on a certain queue: dispatch_queue_t queue = dispatch_queue_create("my.label", DISPATCH

Dispatch group - cannot notify to main thread

て烟熏妆下的殇ゞ 提交于 2019-11-30 12:52:59
问题 After reading Swift 3 evolution on GCD, I am trying to create dispatch group. The problem is the group.notify(queue: do not notify when I pass DispatchQueue.main as a queue, although it does work for background queue. Also I am not sure my syntax is all correct, as I am trying to convert code from Swift 2 to Swift 3. typealias CallBack = (result: Bool) -> Void func longCalculations (completion: CallBack) { let backgroundQ = DispatchQueue.global(attributes: .qosBackground) let group =

Equivalent of GCD serial dispatch queue in iOS 3.x

北城余情 提交于 2019-11-30 12:48:44
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 the recommended way to handle simple background processing (doing web service requests, etc.) in iOS apps