grand-central-dispatch

Using dispatch_once_t per object and not per class

懵懂的女人 提交于 2019-11-28 11:09:05
There are multiple sources calling a particular method, but I would like to ensure that it is called exactly once (per object) I would like to use syntax like // method called possibly from multiple places (threads) -(void)finish { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self _finishOnce]; // should happen once per object }); } // should only happen once per object -(void)_finishOnce{...} Problem is the token is shared accross all instances of the same class - so not a good solution - is there a dispatch_once_t per object - if not what is the best way to ensure it is

Swift, dispatch_group_wait not waiting

。_饼干妹妹 提交于 2019-11-28 10:15:44
I am trying to use grand central dispatch to wait for files to finish download before continuing. This question is a spin-off from this one: Swift (iOS), waiting for all images to finish downloading before returning . I am simply trying to find out how to get dispatch_group_wait (or similar) to actually wait and not just continue before the downloads have finished. Note that if I use NSThread.sleepForTimeInterval instead of calling downloadImage, it waits just fine. What am I missing? class ImageDownloader { var updateResult = AdUpdateResult() private let fileManager = NSFileManager

How do I execute code once and only once in Swift?

北慕城南 提交于 2019-11-28 09:50:38
The answers I've seen so far ( 1 , 2 , 3 ) recommend using GCD's dispatch_once thus: var token: dispatch_once_t = 0 func test() { dispatch_once(&token) { print("This is printed only on the first call to test()") } print("This is printed for each call to test()") } test() Output: This is printed only on the first call to test() This is printed for each call to test() But wait a minute. token is a variable, so I could easily do this: var token: dispatch_once_t = 0 func test() { dispatch_once(&token) { print("This is printed only on the first call to test()") } print("This is printed for each

Process Array in parallel using GCD

痞子三分冷 提交于 2019-11-28 09:12:47
I have a large array that I would like to process by handing slices of it to a few asynchronous tasks. As a proof of concept, I have the written the following code: class TestParallelArrayProcessing { let array: [Int] var summary: [Int] init() { array = Array<Int>(count: 500000, repeatedValue: 0) for i in 0 ..< 500000 { array[i] = Int(arc4random_uniform(10)) } summary = Array<Int>(count: 10, repeatedValue: 0) } func calcSummary() { let group = dispatch_group_create() let queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0) for i in 0 ..< 10 { dispatch_group_async(group, queue, { let

GCD - main vs background thread for updating a UIImageView

前提是你 提交于 2019-11-28 09:06:23
I'm new to GCD and blocks and am easing my way into it. Background: I'm working on a lazy loading routine for a UIScrollView using the ALAssetsLibrary. When my UIScrollView loads I populate it with the aspectRatioThumbnails of my ALAssets and then as the user scrolls, I call the routine below to load the fullScreenImage of the ALAsset that is currently being displayed. It seems to work. (if anyone has a better lazy loading routine please post a comment. I've looked at all I could find plus the WWDC video but they seem to deal more with tiling or have much more complexity than I need) My

How to implement a reentrant locking mechanism in objective-c through GCD?

和自甴很熟 提交于 2019-11-28 08:43:43
I have an objective-c class with some methods, which use a GCD queue to ensure that concurrent accesses to a resource take place serially (standard way to do this). Some of these methods need to call other methods of the same class. So the locking mechanism needs to be re-entrant. Is there a standard way to do this? At first, I had each of these methods use dispatch_sync(my_queue, ^{ // Critical section }); to synchronize accesses. As you know, when one of these methods calls another such method, a deadlock happens because the dispatch_sync call stops the current executing until that other

Create a high priority serial dispatch queue with GCD

天涯浪子 提交于 2019-11-28 08:16:06
How can I create a custom serial queue that runs at high priority? Right now I'm using myQueue = dispatch_queue_create("com.MyApp.MyQueue", NULL); but this doesn't seem to allow for setting a priority? Catfish_Man Create a serial queue, then use dispatch_set_target_queue() to set its target queue to the high priority queue. Here's how: dispatch_set_target_queue(myQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)); Now myQueue should run serially with high priority. Here's another SO answer if you want to know more. The dispatch_queue_attr_make_with_qos_class function may be new

How do I dispatch functions in Swift the right way?

流过昼夜 提交于 2019-11-28 07:00:03
I've kept trying but I just don't get it. I'm rather new to programming so almost every new step is an experiment. Whereas I have no problems dispatching normal closures without arguments/returns, I haven't understood so far how to deal with functions that take (multiple) arguments and return in the end. To get the logic of the proper "work around" it would be great if someone could post a practical example so I could see whether I've got all of it right. I'd be very thankful for any kind of help... If some other practical example illustrate the topic in a better way, please go ahead with your

Objective-C, cancel a dispatch queue using UI event

流过昼夜 提交于 2019-11-28 06:29:52
Scenario: User taps a button asking for some kind of modification on address book. A method is called to start this modification and an alert view is shown. In order to show the alert view and keep the UI responsive, I used dispatch_queue: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_sync(dispatch_get_main_queue(), ^{ // Show the alert view }); }); Start the process of address book modification using: dispatch_async(modifyingAddressBookQueue, ^{}); Now, I want to provide the user with the ability to cancel the process anytime (of course before

dispatch_async and calling a completion handler on the original queue

荒凉一梦 提交于 2019-11-28 05:52:17
I've seen some related questions but none seem to answer this case. I want to write a method that will do some work in the background. I need this method to call a completion callback on the same thread / queue used for the original method call. - (void)someMethod:(void (^)(BOOL result))completionHandler { dispatch_queue_t current_queue = // ??? // some setup code here dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ BOOL ok = // some result // do some long running processing here dispatch_async(current_queue, ^{ completionHandler(ok); }); }); What magic