grand-central-dispatch

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

安稳与你 提交于 2019-11-26 11:39:44
问题 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

Does ARC support dispatch queues?

好久不见. 提交于 2019-11-26 11:33:30
I'm reading apple's documentation about "Memory Management for Dispatch Queues": Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does not support the garbage collection model for reclaiming memory. I know that ARC is not a garbage collector but I'd like to be sure that I don't need to dispatch_retain and dispatch_release my dispatch_queue_t The short answer: YES, ARC retains and releases dispatch queues. And now for the long answer… If your deployment target is lower than iOS 6.0 or

How do I write dispatch_after GCD in Swift 3, 4, and 5?

家住魔仙堡 提交于 2019-11-26 11:30:06
In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // your function here }) But this no longer seems to compile since Swift 3. What is the preferred way to write this in modern Swift? The syntax is simply: // to run something in 0.1 seconds DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code here } Note, the above syntax of adding seconds as a Double seems to be a source of

Can you use cancel/isCancelled with GCD/dispatch_async?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 10:59:39
问题 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 }

NSURLConnection and grand central dispatch

て烟熏妆下的殇ゞ 提交于 2019-11-26 10:09:41
问题 Is it advisable to wrap up NSUrlConnection in a gcd style blocks and run it on a low_priority queue? I need to ensure that my connections are not happening on the main thread and the connections need to be asynchronous. I also need several simultaneous requests to go at once. If I go the gcd route, I\'m not sure which thread the NSUrlConnectionDelegate methods get invoked on. NSURLConnection relies on delegates so once the connection is complete, whatever wrapper class that handles it will

How to dispatch on main queue synchronously without a deadlock?

自作多情 提交于 2019-11-26 10:08:43
问题 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

Core Data and threads / Grand Central Dispatch

送分小仙女□ 提交于 2019-11-26 10:07:10
问题 I\'m a beginner with Grand Central Dispatch (GCD) and Core Data, and I need your help to use Core Data with CGD, so that the UI is not locked while I add 40.000 records to Core Data. I know that CD is not thread safe, so I have to use another context, and then save the data and merge contexts, as far as I was able to understand from some articles. What I couldn\'t do yet, is put the pieces together. So, in my code, I need your help on how to to that. I have: /*some other code*/ for

Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?

怎甘沉沦 提交于 2019-11-26 09:17:32
问题 I have a method that accepts a block and a completion block. The first block should run in the background, while the completion block should run in whatever queue the method was called. For the latter I always used dispatch_get_current_queue() , but it seems like it\'s deprecated in iOS 6 or higher. What should I use instead? 回答1: The pattern of "run on whatever queue the caller was on" is appealing, but ultimately not a great idea. That queue could be a low priority queue, the main queue, or

How to stop a DispatchWorkItem in GCD?

ぃ、小莉子 提交于 2019-11-26 09:02:10
问题 I am currently playing around with Grand Central Dispatch and discovered a class called DispatchWorkItem . The documentation seems a little incomplete so I am not sure about using it the right way. I created the following snippet and expected something different. I expected that the item will be cancelled after calling cancel on it. But the iteration continues for some reason. Any ideas what I am doing wrong? The code seems fine for me. @IBAction func testDispatchItems() { let queue =

Dispatch queues: How to tell if they're running and how to stop them

微笑、不失礼 提交于 2019-11-26 08:48:53
问题 I\'m just playing around with GCD and I\'ve written a toy CoinFlipper app. Here\'s the method that flips the coins: - (void)flipCoins:(NSUInteger)nFlips{ // Create the queues for work dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL); // Split the number of flips into whole chunks of kChunkSize and the remainder. NSUInteger numberOfWholeChunks = nFlips / kChunkSize; NSUInteger