grand-central-dispatch

Swift, dispatch_group_wait not waiting

耗尽温柔 提交于 2019-12-17 19:25:10
问题 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

How to ensure to run some code on same background thread?

左心房为你撑大大i 提交于 2019-12-17 19:24:54
问题 I am using realm in my iOS Swift project. Search involve complex filters for a big data set. So I am fetching records on background thread. But realm can be used only from same thread on which Realm was created. I am saving a reference of results which I got after searching Realm on background thread. This object can only be access from same back thread How can I ensure to dispatch code at different time to the same thread? I tried below as suggested to solve the issue, but it didn't worked

What's the difference between the “global queue” and the “main queue” in GCD?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 17:26:03
问题 Among some other ways, there are these two ways to get queues in GCD : dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_get_main_queue(); If I'm not completely wrong, the "main queue" is executing on the main thread and is good for "callback" blocks which execute UI work. Does this mean a "global queue" is one that runs on a background thread? 回答1: The main queue does indeed run on the main thread like you say. The global queues are concurrent queues and from the main

How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

落爺英雄遲暮 提交于 2019-12-17 09:18:13
问题 Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function could be called from something already running on that queue, so I needed to check for this case in order to prevent a deadlock from a synchronous dispatch to the same queue. I used code like the following to do this: void runSynchronouslyOnVideoProcessingQueue(void (^block)(void)) { dispatch

Whither dispatch_once in Swift 3?

て烟熏妆下的殇ゞ 提交于 2019-12-17 04:56:10
问题 Okay, so I found out about the new Swifty Dispatch API in Xcode 8. I'm having fun using DispatchQueue.main.async , and I've been browsing around the Dispatch module in Xcode to find all the new APIs. But I also use dispatch_once to make sure that things like singleton creation and one-time setup don't get executed more than once (even in a multithreaded environment)... and dispatch_once is nowhere to be found in the new Dispatch module? static var token: dispatch_once_t = 0 func whatDoYouHear

In Swift how to call method with parameters on GCD main thread?

血红的双手。 提交于 2019-12-17 04:43:10
问题 In my app I have a function that makes an NSRURLSession and sends out an NSURLRequest using sesh.dataTaskWithRequest(req, completionHandler: {(data, response, error) In the completion block for this task, I need to do some computation that adds a UIImage to the calling viewcontroller. I have a func called func displayQRCode(receiveAddr, withAmountInBTC:amountBTC) that does the UIImage-adding computation. If I try to run the view-adding code inside of the completion block, Xcode throws an

Do you need to create an NSAutoreleasePool within a block in GCD?

浪子不回头ぞ 提交于 2019-12-17 03:49:43
问题 Normally, if you spawn a background thread or run an NSOperation on an NSOperationQueue you need to create an NSAutoreleasePool for that thread or operation because none exists by default. Does the same rule apply to a block that is placed within a Grand Central Dispatch queue and will be run on a non-main thread? That is, do you need to create an NSAutoreleasePool within each block you dispatch to anything other than the main queue? In my limited testing, I don't see the console warnings for

Waiting until two async blocks are executed before starting another block

ⅰ亾dé卋堺 提交于 2019-12-16 22:09:28
问题 When using GCD, we want to wait until two async blocks are executed and done before moving on to the next steps of execution. What is the best way to do that? We tried the following, but it doesn't seem to work: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block1 }); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { // block2 }); // wait until both the block1 and block2 are done before start block3 // how to do that? dispatch

sync vs async firebase operations for swift 4?

房东的猫 提交于 2019-12-14 04:06:42
问题 Are all operations and queries onto a Firebase realtime database asynchronous or synchronous or both? In addition to this, what about Firebase authentication? So I guess my question is: Do I need to put Firebase operations into a concurrent queue, or is it okay just leaving it in the main queue? 回答1: The thing about asynchronous programming is that it’s not really intuitive at first. If you want to fetch some data, it’s natural to want to write code that’s structured something like this: try

How is dispatch_debug supposed to be used?

给你一囗甜甜゛ 提交于 2019-12-14 03:58:33
问题 I am struggling with a deadlock in my GCD code. Then I saw this function dispatch_debug in the header file <dispatch/object.h> . /*! * @function dispatch_debug * * @abstract * Programmatically log debug information about a dispatch object. * * @param object * The object to introspect. * * @param message * The message to log above and beyond the introspection. */ __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW __attribute__((__format__