grand-central-dispatch

How to dispatch on main queue synchronously without a deadlock?

爱⌒轻易说出口 提交于 2019-11-27 06:00:54
I need to dispatch a block on the main queue, synchronously. I don’t know if I’m currently running on the main thread or no. The naive solution looks like this: dispatch_sync(dispatch_get_main_queue(), block); But if I’m currently inside of a block running on the main queue, this call creates a deadlock. (The synchronous dispatch waits for the block to finish, but the block does not even start running, since we are waiting for the current one to finish.) The obvious next step is to check for the current queue: if (dispatch_get_current_queue() == dispatch_get_main_queue()) { block(); } else {

Using dispatch_once_t per object and not per class

匆匆过客 提交于 2019-11-27 05:58:42
问题 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

using dispatch_sync in Grand Central Dispatch

假如想象 提交于 2019-11-27 05:54:36
Can anyone explain with really clear use cases what the purpose of dispatch_sync in GCD is for? I can't understand where and why I would have to use this. Thanks! David Gelhar You use it when you want to execute a block and wait for the results. One example of this is the pattern where you're using a dispatch queue instead of locks for synchronization. For example, assume you have a shared NSMutableArray a , with access mediated by dispatch queue q . A background thread might be appending to the array (async), while your foreground thread is pulling the first item off (synchronously):

How to asynchronous load image from a web-server in UICollectionView using NSCache

跟風遠走 提交于 2019-11-27 05:39:59
I have some issues when loading images from a web-server in UICollectionView using NScache. The problem: The images are not proper displayed: sometimes they are not showned in the corresponding cell or the image is changing on scroll Situation: I have 3 arrays whitch are properly loaded from the web-server in function viewDidLoad(). These arrays are: vPrice, vTitle and vImages_api my custom class for cell have: label for price: cell.lblPrice label for title: cell.lblTitle image: cell.imgPicture I belive that the problem is in function func collectionView(_ collectionView: UICollectionView,

Perform UI Changes on main thread using dispatch_async or performSelectorOnMainThread? [duplicate]

こ雲淡風輕ζ 提交于 2019-11-27 05:36:51
问题 Possible Duplicate: Grand Central Dispatch (GCD) vs. performSelector - need a better explanation To execute "stuff" on the main thread, should I use dispatch_async or performSelectorOnMainThread ? Is there a preferred way, right/or wrong, and/or best practice? Example: I'm performing some logic within the block of an NSURLConnection sendAsynchronousRequest:urlRequest method. Because I'm doing stuff to the main view such as presenting a UIAlertView I need to show the UIAlertView on the main

Possible to reset state of dispatch_once in unit test, to make them run again

我只是一个虾纸丫 提交于 2019-11-27 05:34:02
问题 Is it possible to reset the state of dispatch_once code in a unit test tearDown? I think it would be nice if our unit tests could run from a really clean state, but we are struggling with dispatch_once and some singletons made with dispatch once. 回答1: I should note first that this isn't a good thing to do in any situation other than testing; even then, proceed with care -- AliSoftware provides some details and example code in comments below. See also the interesting answers at Can I declare a

dispatch_async vs. dispatch_sync using Serial Queues in Grand Central Dispatch

霸气de小男生 提交于 2019-11-27 05:22:29
问题 OK, I love Grand Central Dispatch and after using it with relative success but this is something I don't fully understand. Suppose I have created my own serial queue using dispatch_queue_t myQueue; myQueue = dispatch_queue_create("myQueue", NULL); After that I do this: dispatch_async(myQueue, ^{ [self doStuff1]; }); // and a few lines later... dispatch_sync(myQueue, ^{ [self doStuff2]; }); The first dispatch is async. So, it will be done concurrently, right? How can that be if myQueue is

Wait for async task to finish completion block before returning in app delegate

不羁的心 提交于 2019-11-27 05:08:22
问题 I'm using a subclass of UIManagedDocument to use Core Data in my project. The point is for the subclass to return a singleton instance so that my screens can simply call it and the managed object context remains the same for all of them. Before using the UIManagedDocument , I need to prepare it by opening it if its file path already exists, or creating it if it doesn't yet. I created a convenience method prepareWithCompletionHandler: in the subclass to facilitate both scenarios.

Can you use cancel/isCancelled with GCD/dispatch_async?

匆匆过客 提交于 2019-11-27 04:01:25
I've been wondering, can you use cancel/cancelAllOperations/.isCancelled with a thread you have launched with GCD? Currently, I just use a boolean as a flag, to cancel the background process. Let's say you want to do a lot of processing in the background, while keeping the UI responsive so that you can catch a cancel button (or animate something to show the processor is working). Here's how we do it... @interface AstoundingView : UIView { BOOL pleaseAbandonYourEfforts; blah } @implementation AstoundingView // // these are the foreground routines... // begin, abandon and all-done // -(void

dispatch_once after the Swift 3 GCD API changes

北战南征 提交于 2019-11-27 02:45:39
What is the new syntax for dispatch_once in Swift after the changes made in language version 3? The old version was as follows. var token: dispatch_once_t = 0 func test() { dispatch_once(&token) { } } These are the changes to libdispatch that were made. From the doc : Dispatch The free function dispatch_once is no longer available in Swift. In Swift, you can use lazily initialized globals or static properties and get the same thread-safety and called-once guarantees as dispatch_once provided. Example: let myGlobal = { … global contains initialization in a call to a closure … }() _ = myGlobal /