grand-central-dispatch

How can i wait to receive a response from a DispatchWorkItem before moving on to the next request or next DispatchWorkItem in a Dispatch Queue

走远了吗. 提交于 2019-12-08 12:17:47
问题 I have an array of dispatch workItems, how to wait until one work is completed before i move on to the next work in the queue? func AsyncCalls(statusHandler: @escaping (String) -> Void){ var dispatchWorkItems : [DispatchWorkItem] = [] let categoryWorkItem = DispatchWorkItem { main { return statusHandler("Loading categories ") } self.modelView.getCategories(completion: { data,error in main { if data.isEmpty { return statusHandler("\(error )") }else{ return statusHandler("Done loading

Difference Between DispatchQueue.sync vs DispatchQueue.async

天涯浪子 提交于 2019-12-08 10:46:56
问题 Im trying to understand the Dispatch Sync and Dispatch Async, I know that its executes in sync and async manner of GCD. But when i try the below code it gave me weird scenario. I testing the below code in Playground and Sync block executed 3times and the async block gave the NSException. //: A UIKit based Playground for presenting user interface import UIKit import PlaygroundSupport class MyViewController : UIViewController { override func loadView() { let view = UIView() view.backgroundColor

Is there a specific way to append DispatchWorkItems to a DispatchQueue instead of re declaring them in code?

大憨熊 提交于 2019-12-08 09:31:17
问题 I have several Dispatch work items to execute on a queue i don't want to redeclare the codes, i want to pass them to an array or list of DispatchWorkItems and then inject it to a dispatch queue is there any way to achieve this ? func executeDispatchWorkItem(url: String, completion : @escaping (Result<String,Error>)-> Void,beganHandler : @escaping (String)-> Void){ do { beganHandler("\(url) Began to execute ") let content = try String(contentsOf:URL(string: url)!) completion(.success(content))

dispatch_queue_t SerialQueue how check if is still running. ios GCD

谁都会走 提交于 2019-12-08 08:59:51
问题 I need to do a validation and for this I need to know if the task is still running i use this code for launch the task ` SerialQueue = dispatch_queue_create("miColaEnSerie", NULL); dispatch_async(SerialQueue, ^{ [self loadImageFriend:init finalWhitNumber:final img1WithArray:infoImages1 img2WithArray:infoImages2 img3WithArray:infoImages3]; });` 回答1: If you have only one task at a time in your SerialQueue then you can add atomic property to a class like: @property (assign) BOOL

How does the dispatcher work when mixing sync/async with serial/concurrent queue?

流过昼夜 提交于 2019-12-08 08:09:54
问题 In Grand Central Dispatch, how does the dispatcher work with different queues ( serial and concurrent ) when using the dispatch_sync function and the dispatch_async function? 回答1: First of all we need two type of queue : one serial and one concurrent : dispatch_queue_t serialQueue = dispatch_queue_create("com.matteogobbi.dispex.serial_queue", DISPATCH_QUEUE_SERIAL); dispatch_queue_t concurrentQueue = dispatch_queue_create("com.matteogobbi.dispex.concurrent_queue", DISPATCH_QUEUE_CONCURRENT);

When creating thread safe reads in Swift, why is a variable create outside the concurrent queue?

痞子三分冷 提交于 2019-12-08 08:04:14
问题 public class Account { // MARK: Initializer // Custom initializer // MARK: Stored Properties let concurrentQueue: DispatchQueue = DispatchQueue( label: "concurrentQueue", qos: DispatchQoS.userInitiated, attributes: [DispatchQueue.Attributes.concurrent] ) private var _name: String public name: String { get { return self.concurrentQueue.sync { return self._name } } set { self.concurrentQueue.async(flags: .barrier) { self._name = newValue } } } } Let's say you have a class like above where you

Running multiple background threads iOS

情到浓时终转凉″ 提交于 2019-12-08 07:41:29
Is it possible to run multiple background threads to improve performance on iOS . Currently I am using the following code for sending lets say 50 network requests on background thread like this: dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ // send 50 network requests }); EDIT: After updating my code to something like this no performance gain was achieved :( Taken from here dispatch_queue_t fetchQ = dispatch_queue_create("Multiple Async Downloader", NULL); dispatch_group_t fetchGroup = dispatch_group_create(); // This will allow up to 8 parallel

synchronized block within dispatch_async

有些话、适合烂在心里 提交于 2019-12-08 06:35:44
问题 I have seen code that dispatch async to a main queue or private dispatch queue (serial) and then in the dispatch code block is @synchronized. Under what circumstance do you want to do that? Isn't a serial queue already providing the synchronization needed? Can the synchronized block be replaced with another GCD dispatch? 回答1: @synchronized() ensures that contained code (for a given token as the argument to @synchronized ) is only run on one thread at a time. Blocks submitted to a serial queue

Objective-C – Structuring GCD code for background processing

混江龙づ霸主 提交于 2019-12-08 06:01:51
问题 I have some code that takes a little time to process and thus appropriately it should not run on the main queue. However I'm not sure on how to correctly "structure" the GCD code segments. I.e every time the app becomes active I'm doing a syncing operation: AppDelegate.m - (void)applicationDidBecomeActive:(UIApplication *)application { AddressBookHelper *abHelper = [AddressBookHelper sharedInstance]; // singleton helper class of NSObject [abHelper sync]; } The syncing code inside

C++11 app that uses dispatch_apply not working under Mac OS Sierra

人盡茶涼 提交于 2019-12-08 05:23:23
问题 I had a completely functioning codebase written in C++11 that used Grand Central Dispatch parallel processing, specifically dispatch_apply to do the basic parallel for loop for some trivial game calculations. Since upgrading to Sierra, this code still runs, but each block is run in serial -- the cout statement shows that they are being executed in serial order, and CPU usage graph shows no parallel working on. Queue is defined as: workQueue = dispatch_queue_create("workQueue", DISPATCH_QUEUE