grand-central-dispatch

GCD Dispatched Operation Not Running When App is Placed in Background

与世无争的帅哥 提交于 2019-12-10 19:47:46
问题 I launch a method that is, essentially, an endless loop using dispatch_queue_create and then dispatch_async (and then the code-loop is inside the dispatched block). The loop runs perfectly. However, when the application gets backgrounded, it pauses. Then it restarts when the app takes the foreground. How can I prevent this from happening? I've been looking here but it seems that the priority is not one of the things I can choose. 回答1: Use the -[UIApplication

GCD and Threads

空扰寡人 提交于 2019-12-10 18:58:45
问题 I want to understand something about GCD and Threads. I have a for loop in my view controller which asks my model to do some async network request. So if the loop runs 5 times, the model sends out 5 network requests. Is it correct to state that 5 threads have been created by my model considering the fact that I'm using NSURLConnection's sendAsyncRequest and the completion handlers will be called on an additional 5 threads ? Now, If I ask my view controller to execute this for loop on a

What is the proper way to work with AWSTask objects in Swift?

心已入冬 提交于 2019-12-10 18:24:29
问题 Hello and thanks in advance for your time. In my code I am making various requests to AWSSQS which all return AWSTask. I have found working with these AWSTask objects to be very difficult while also trying to keep all the logic specific to AWS in a single class so I can easily switch to a different cloud service if need be. Ideally, what I would like to do is execute a series of AWS tasks asynchronously in a serial fashion. Normally I would just add tasks to a custom Serial Dispatch Queue but

dispatch_after versus performSelector afterDelay

情到浓时终转凉″ 提交于 2019-12-10 14:58:35
问题 I am writing a game with sliding blocks. In order to shuffle the game from the solved state, I would like to repeatedly call pushRandomPiece at regular interval to visually shuffle the game. I wanted to use dispatch_after in the first place but I have a problem with the firing date: This works: -(void)shuffle { for (int i=0; i<50;i++) [self performSelector:@selector(pushRandomPiece) withObject:nil afterDelay:i*0.50*2]; } The difference between two consecutive calls in pushRandomPiece is

Multithreading and autorelease pool

独自空忆成欢 提交于 2019-12-10 14:28:05
问题 As I'm mastering my skills with multithreading with GCD, I've come across some question. Suppose you have the following method: - (void)method { NSString *string= [NSString string]; //will be autoreleased dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //very very lengthy operation... NSLog(@"%@", string); //is it safe? }); } I'm wondering if this is correct, because I think I should have retained string before the block execution: in fact I fear that the

Swift app using DispatchQueue.concurrentPerform(iterations:) no longer runs concurrently under Mac OS Sierra

时光总嘲笑我的痴心妄想 提交于 2019-12-10 14:23:52
问题 In testing my code under Sierra, I found that the methods that previously handled concurrent queues were no longer working. In analyzing the error in my C++ codebase, one of the users suggested a workaround that involved explicitly naming a target for the queue declaration (see this post: C++11 app that uses dispatch_apply not working under Mac OS Sierra ) that seems to have solved the problem. In Swift 3, the following code would be used to execute a closure concurrently, but it is

GCD and callbacks - concurrency issue

冷暖自知 提交于 2019-12-10 14:11:57
问题 I have a callback handler registered that listens to changes in the iOS Address Book. Due to some strange reason (for which a bug has been filed), this callback can sometimes be called more than once when the app returns from the background. I want my callback handler to run it's logic only once, even in cases the callback is called multiple times. This is how I register the callback: ABAddressBookRegisterExternalChangeCallback(address_book, adressBookChanged, self); This is how I structured

Difference between dispatching to a queue with `sync` and using a work item with a `.wait` flag?

雨燕双飞 提交于 2019-12-10 13:18:21
问题 I'm learning about Apple's GCD, and watching the video Concurrent Programming With GCD in Swift 3. At 16:00 in this video, a flag for DispatchWorkItem is described called .wait , and the functionality and diagram both show exactly what I thought myQueue.sync(execute:) was for. So, my question is; what is the difference between: myQueue.sync { sleep(1); print("sync") } And: myQueue.async(flags: .wait) { sleep(1); print("wait") } // NOTE: This syntax doesn't compile, I'm not sure where the `

launchd: sleep in GCD managed signal handler

那年仲夏 提交于 2019-12-10 11:39:34
问题 I encounter a strange situation in a launchd managed daemon when I try to sleep in the SIGTERM handler that is managed with Grand Central Dispatch as described here. Everything works fine and I do get a SIGTERM signal handler before receiving a SIGKILL when I do not sleep in the SIGTERM handler. But as soon as I do sleep -- even for extremly short amounts of time like a usleep(1); -- I do not get a SIGTERM handler at all but instead my daemon is SIGKILLed instantly by launchd. Btw I am using

how to create a serial queue in swift4 [duplicate]

感情迁移 提交于 2019-12-10 11:15:39
问题 This question already has answers here : How to create dispatch queue in Swift 3 (15 answers) Type 'DispatchQueue.Attributes' has no member 'serial' (1 answer) Closed last year . DispatchQueue.init(label: , qos: , attributes: , autoreleaseFrequency: , target: ) How to write the parameters, I looked at the original notes for a long time,But I still can't write a serial queue 来源: https://stackoverflow.com/questions/50129528/how-to-create-a-serial-queue-in-swift4