grand-central-dispatch

When are GCD queues used and when do you know you need them? Swift

三世轮回 提交于 2020-01-25 04:02:29
问题 After reading about Concurrent and Serial queues, sync and async, I think I have an idea about how to create queues and the order they are executed in. My problem is that in any of the tutorials I have seen, none of them actually tell you many use cases. For example: I have a network manager that uses URLSessions and serializes json to send a request to my api. Does it make sense to wrap it in a .utility Queue or in a .userInitiated or do I just don't wrap it in a queue. let task = LoginTask

How to parallelize many (100+) tasks without hitting global GCD limit?

谁说胖子不能爱 提交于 2020-01-25 01:25:10
问题 The problem : When lazy-loading a list of 100+ icons in the background, I hit the GCD thread limit (64 threads), which causes my app to freeze up with a semaphore_wait_trap on the main thread. I want to restructure my threading code to prevent this from happening, while still loading the icons asynchronous to prevent UI blocking. Context : My app loads a screen with SVG icons on it. The amount differs on average from 10-200. The icons get drawn by using a local SVG image or a remote SVG image

How to dispatch_group_wait for dispatch_group_async inside an asynchronous block

笑着哭i 提交于 2020-01-21 08:38:09
问题 I have code that looks something like this: [SVProgressHUD show]; [imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, ...) { dispatch_group_async(queueGroup, queue, ^{ // Do stuff }); }]; dispatch_group_wait(queueGroup, DISPATCH_TIME_FOREVER); [SVProgressHUD dismiss]; Basically, display a loading animation HUD and start generating image thumbnails from an asset, then once it's done hide the HUD. I'm using a dispatch group since i want to make

How can I create a reference cycle using dispatchQueues?

本秂侑毒 提交于 2020-01-19 06:09:06
问题 I feel that I've always misunderstood that when reference cycles are created. Before I use to think that almost any where that you have a block and the compiler is forcing you to write .self then it's a sign that I'm creating a reference cycle and I need to use [weak self] in . But the following setup doesn't create a reference cycle. import Foundation import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution class UsingQueue { var property : Int = 5 var queue : DispatchQueue?

looking for a specific example where Operation is preferred over GCD or vice-versa

人盡茶涼 提交于 2020-01-17 15:31:36
问题 this question is not about what is the difference between operation queues and dispatch queues. i know that. i have gone through it. but still in mind, i don't have any such example where i can 100% say that, yeah GCD should be only choice for it. or OperationQueue should be perfect choice for it. can you give me some example, which explains clear priority of one over another? because almost everything which can be done by gcd, can be also done by operation queue. 回答1: Nothing’s 100%, but

looking for a specific example where Operation is preferred over GCD or vice-versa

有些话、适合烂在心里 提交于 2020-01-17 15:28:08
问题 this question is not about what is the difference between operation queues and dispatch queues. i know that. i have gone through it. but still in mind, i don't have any such example where i can 100% say that, yeah GCD should be only choice for it. or OperationQueue should be perfect choice for it. can you give me some example, which explains clear priority of one over another? because almost everything which can be done by gcd, can be also done by operation queue. 回答1: Nothing’s 100%, but

Need clarification on dispatch_group_wait() behavior when dispatch_group_create() and dispatch_group_enter() are called from different queues

风格不统一 提交于 2020-01-17 01:31:06
问题 I am looking at the Ray Wenderlich tutorial on using dispatch queues to get notified when a group of tasks complete. http://www.raywenderlich.com/63338/grand-central-dispatch-in-depth-part-2 The first code shown under "Code that works" is straight from the tutorial. The Alert view(final completion block) get executed after all 3 downloads complete. I tried to play around with it and moved the dispatch async down in the "Code that does not work" to see what will happen if dispatch_group_create

How do I ensure my DispatchQueue executes some code on the main thread specifically?

对着背影说爱祢 提交于 2020-01-16 08:40:09
问题 I have a singleton that manages an array. This singleton can be accessed from multiple threads, so it has its own internal DispatchQueue to manage read/write access across threads. For simplicity we'll say it's a serial queue. There comes a time where the singleton will be reading from the array and updating the UI. How do I handle this? Which thread my internal dispatch queue is not known, right? It's just an implementation detail I'm to not worry about? In most cases this seems fine, but in

How to avoid nested blocks

只愿长相守 提交于 2020-01-16 06:41:03
问题 GCD and blocks are so nice and convenient. But when I fall in love with it, I found that something bad happened. Look at these codes below: [self functionA:^(BOOL success) { if (success) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [self functionB:^(NSError *error) { if (error != nil) { dispatch_async(dispatch_get_main_queue(), ^(void) { [self functionC:^(id result) { if (result) { [self functionD:^(BOOL success) { if (success) { [self DoSomething]

How to avoid nested blocks

雨燕双飞 提交于 2020-01-16 06:39:32
问题 GCD and blocks are so nice and convenient. But when I fall in love with it, I found that something bad happened. Look at these codes below: [self functionA:^(BOOL success) { if (success) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [self functionB:^(NSError *error) { if (error != nil) { dispatch_async(dispatch_get_main_queue(), ^(void) { [self functionC:^(id result) { if (result) { [self functionD:^(BOOL success) { if (success) { [self DoSomething]