Questions tagged [grand-central-dispatch]

Grand Central Dispatch (GCD) provides a simple and robust mechanism for concurrent and asynchronous operations, primarily in Apple operating systems (e.g., iOS, macOS, watchOS, and tvOS), but also FreeBSD and MidnightBSD.

Filter by
Sorted by
Tagged with
0 votes
1 answer
51 views

Will DispatchQueue.global(qos: .background).async and DispatchQueue.main.async get cancelled when in background or foreground? [closed]

I am using DispatchQueue.global(qos: .background).async and DispatchQueue.main.async for firebase token updation. But I feel like the code block written inside DispatchQueue.global(qos: .background)....
Aswin Rathees's user avatar
2 votes
1 answer
63 views

Proper use of strongSelf / weakSelf in nested dispatch_after blocks

I have a complicated launch and I need to present and dismiss multiple view controllers. I have this bit of code where I need to use delays in presentation; I was wondering if my use of strongSelf / ...
SAHM's user avatar
  • 4,119
1 vote
1 answer
66 views

Does a dispatch group block the main thread in iOS?

I'm calling some code to query and update the CallKit call extension, and very occassionally I've seen a hang and the call is timing out. I thought I'd put this off the main thread so that it wouldn't ...
GruntBiscuits's user avatar
-1 votes
1 answer
92 views

What could cause different output for the same code?

Why does this piece of code produce different output when run in Xcode and Swift Playground? for i in 0...10 { DispatchQueue.global().async { DispatchQueue.global().sync { ...
user avatar
0 votes
1 answer
52 views

UITableViewCell image from url is wrong at first load

Situation There's something wrong with setting url image to UITableViewCell. I created CancelableImageView which is custom UIImageView to cancel image downloading and setting task when new image is ...
Dan Choi's user avatar
2 votes
1 answer
87 views

What is the difference between these DispatchTime calculations?

I ran into this line code in AlamofireImage's older version. let tinyDelay = DispatchTime.now() + Double(Int64(0.001 * Float(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) // Need to let the runloop cycle ...
Ahmet Sancar's user avatar
-2 votes
1 answer
74 views

I need to wait for a bunch of network APIs to complete. How do I do that? [duplicate]

I am writing an app that needs to make a number of network API calls (which all complete asynchronously, and wait until they all complete before moving on to the next step in my code. How do I do that?...
Duncan C's user avatar
  • 129k
0 votes
2 answers
305 views

Why and how do 'Data(contentsOf: URL) call' and 'URLSession.shared.data(from: URL) call' each as a child task to a TaskGroup make a difference?

I am trying to build a chat app. I request the messages through API. Since the chat might be an image(a URL for that image in this case), and every image has different height / width ratio, I’m trying ...
Cupertino's user avatar
2 votes
1 answer
176 views

Swift Concurrency - Sync and Async Task

Using GCD, I am able to run a synchronous task after an asynchronous task has finished. let queue = DispatchQueue(label: "for.test") var exampleList = [String]() queue.async { exampleList....
Shape Fit's user avatar
0 votes
2 answers
71 views

Race Condition & Recursion: Dictionary Is Not Returned Before Next Line Executes

Desired Outcome Initialize MyClass with fixedValues as the dictionaryRepresentation argument. Set fixed values before executing the init line Problem fixedValues isn't being set before the self....
Tristan Newman's user avatar
0 votes
1 answer
123 views

Swift make function work like serial queue

I have a function sensMessage in my iOS Project, the function does some logic and then sends a network call, I want to make it work like a queue so that when the completion handler is called another ...
YosiFZ's user avatar
  • 7,812
0 votes
0 answers
67 views

Robots Board problem iOS - Multithreading

Im trying to create a robots challenge board game, but im really struggling with the round and thread logic. Some suppositions: 1- Its a 7x7 board game 2- At the start of each round, randomly place a ...
Vinícius Albino's user avatar
0 votes
0 answers
54 views

I am having trouble updating MBProgressHUD in dispatch main thread

I'm trying to update MBProgressHUD in my controller. From only showing a static message, i want to put either progressbar/progress percentage to show uploading process to user. YBWeakSelf; [[...
Christopher Sonny's user avatar
1 vote
2 answers
110 views

Why can DispatchQueue.main.async be used as an input source for RunLoop.current.run?

import Dispatch import Foundation DispatchQueue.main.async { print("just print in main async") } RunLoop.current.run(mode: .default, before: .distantFuture) print("RunLoop....
eczn's user avatar
  • 1,529
2 votes
0 answers
45 views

swift DispatchQueue.main.asyncAfter doesn't work in command line? [duplicate]

import Foundation import Dispatch DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { print("fffffff") } run by swift but program ends without output:
eczn's user avatar
  • 1,529
0 votes
2 answers
64 views

iOS - Adding delay to a for loop

I am trying to execute a series of tasks with a delay. To experiment that, I created a simple app with two labels and a button to perform hit test. Upon hitting the "Hit me" button, yellow ...
iOS's user avatar
  • 3,590
0 votes
2 answers
51 views

Question about dispatch_group_t results in two callbacks in a network request

I use dispatch_group_t to handle my three network requests with the following code: dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(0, 0); [self....
HR Jackson's user avatar
2 votes
1 answer
240 views

In Swift, why is a custom serial queue with a global queue as its target not executed concurrently?

This is my first question here, so please be nice. I struggle to understand the dynamics around a queue's target in Swift: I have read that a custom queue inherits the behaviour of its target queue. ...
Stephanovits's user avatar
0 votes
1 answer
185 views

Using `DispatchQueue.concurrentPerform()` inside a Swift actor

I've a Swift actor that has a long-running update method that mutates a lot of shared state. I want to perform the long-running work in parallel on a background queue, but avoid the update function ...
Remco Poelstra's user avatar
3 votes
2 answers
149 views

Why is this Swift Readers-Writers code causing deadlock?

I seem to have a classic solution for Readers-Writers problem in Swift using concurrent DispatchQueue with a barrier for writes. However, then I run my test code, it deadlocks. Here's my wanna-be-...
Alex's user avatar
  • 1,594
0 votes
3 answers
206 views

What is the difference between DispatchQueue.global().sync vs main thread

what is the difference between below 2 piece of code while output is same for both print("1") print("k") print("2") print("3") print("4") print("...
neeraj joshi's user avatar
8 votes
1 answer
1k views

DispatchQueue.main.asyncAfter equivalent in Structured Concurrency in Swift?

In GCD I just call: DispatchQueue.main.asyncAfter(deadline: .now() + someTimeInterval) { ... } But we started to migrate to Structured Concurrency. I tried the following code: extension Task where ...
Gargo's user avatar
  • 1,177
0 votes
1 answer
51 views

How to convert dispatch_time_t to CFAbsoluteTime or struct timespec?

While using Apple's grand central dispatch (GCD) for a timer one can see dispatch_time_t 64-bit integer that holds the time for GCD. How do you convert its value to CFAbsoluteTime or struct timespec ...
c00000fd's user avatar
  • 21.4k
0 votes
1 answer
103 views

Swift serial DispatchQueue not executing serially

Hey in my code the function "sendRequest" is called multiple times which sends a request to a server. The server processes all requests one after the other and sends back a multiple ...
Takrem's user avatar
  • 51
1 vote
1 answer
72 views

Hide loading indicator on debounced search

I have a VC with a list of different options like sliders, buttons, switches and etc (basically it's a common Filter page). I have a serial queue and i run my api call once 0.5 seconds just to avoid ...
Bobby Redjeans's user avatar
1 vote
0 answers
98 views

Is it possible to create an NSWindow from the main thread in dart macos?

I'm currently trying to create a native NSWindow under MacOS using dart (directly, not flutter) in combination with obj-c. The problem is that all UI things need to be executed on the system main ...
Sanatorius's user avatar
0 votes
1 answer
36 views

AVAudioConverter buffers are over-retained in a DispatchQueue.concurrentPerform context

I have an audio analysis tool which I’m trying to parallelise, but it uses an escalating amount of heap space when working on a set of audio files. It turns out that when I use AVAudioConverter....
ibsh's user avatar
  • 31
-3 votes
1 answer
59 views

Timer does not get called inside Grand Central Dispatch async [duplicate]

I have a simple timer each 1 second will execute logging print inside an async Grand Central Dispatch: import Foundation do { let GDC = DispatchQueue.init(label: "GDC", attributes: ....
Kim Mỹ's user avatar
  • 378
0 votes
2 answers
155 views

Multithreading/QOS for Timer

For timer related operations is it worth using any QOS? If yes then which thread should be used or which QOS can we use? Now here I am creating a Timer and adding it to Runloop: var activityTimer = ...
Raghav N's user avatar
1 vote
1 answer
71 views

Can DispatchQueue be used to queue user inputs?

In my game, each time a user swipes in a particular direction something on screen animates. Since each animation takes about a second, if the user rapidly swipes on the screen I need a way to "...
rayaantaneja's user avatar
  • 1,424
0 votes
1 answer
41 views

applicationWillTerminate deadlock

I want to close all VZVirtualMachine instances when my app is terminating. Im using this code in the AppDelegate func applicationWillTerminate(_ notification: Notification) { let runningEntities = ...
Damian Dudycz's user avatar
0 votes
1 answer
291 views

Why doesn't customSerialQueue.sync inside DispatchQueue.main.async deadlock, while DispatchQueue.main.sync does?

I have two code blocks, one using DispatchQueue.main.sync inside DispatchQueue.main.async, and another using a customSerialQueue.sync instead of DispatchQueue.main.sync. The first block causes a ...
shinhong's user avatar
  • 406
1 vote
1 answer
270 views

Proper iOS DispatchQueue usage?

I'm trying to extend the Landmark SwiftUI Tutorials to persist data. The code I added getDataFromFileURL draw inspiration from Persisting data tutorial. I expect for the first time, the json files won'...
Patrick's user avatar
  • 4,206
1 vote
3 answers
690 views

How should I convert a method with a callback queue to async/await?

I want to wrap existing GCD based functions that use both callbacks and callback queues with async/await. I was wondering what pattern I should follow regarding the callback queue. ie: // This is what ...
Guig's user avatar
  • 9,941
0 votes
0 answers
211 views

How to use DispatchQueue to wait for a file to be created in Swift

I'm developing an app that needs to wait for files to be created in the disk in order to continue. After obtaining the array of paths from the json file, I need the app to: Show a progress bar Wait ...
fdeis's user avatar
  • 1
-3 votes
1 answer
97 views

Why does the main thread guarantee the UIView.animate method? [closed]

I wonder how the UIView.animate method works. because just UI method must be used from main thread only? try it UIView.animate method was called without Dispatchqueue.main.async so, I check Thread....
Rx_'s user avatar
  • 1
1 vote
0 answers
202 views

Understanding the idea of asyncAndWait

I have found some API lacking documentation and I'm hoping someone out there understands what it's about. https://developer.apple.com/documentation/dispatch/dispatchqueue/3656285-asyncandwait ...
bobby123uk's user avatar
1 vote
0 answers
93 views

Why is the main queue misbehaving inside dispatchMain()?

The following program: import Foundation DispatchQueue.main.async { print("(1) on main thread: \(Thread.isMainThread)") DispatchQueue.main.async { // executed on a ...
Valentin Milea's user avatar
0 votes
1 answer
646 views

what is the difference between dispatch work item and dispatch group

func run() { // create a work item with the custom code timeoutWorkItem = DispatchWorkItem { // Insert your code here var retryNum: Int = 0 ...
Joice George's user avatar
2 votes
1 answer
263 views

Observable sequence called several times on retryWhen, but should call only once

I am trying to build RxSwift Auth token refresh service using following tutorial: https://www.donnywals.com/building-a-concurrency-proof-token-refresh-flow-in-combine/. However, I faced with issue, ...
Richard's user avatar
  • 99
-2 votes
1 answer
236 views

What does exclamation in purple mean in Xcode?

What does the exclamation in purple mean?
Joice George's user avatar
0 votes
1 answer
75 views

GCD (C++) Parallel looping Errors: Bug in Code, or Bug in Grand Central Dispatch?

The following snippet performs a set of polynomials on a vector of values read in from a TSV file, eg.. 0.335832971253701 0.111283951925475 0.28470219633399 0.237865566116303 0.936298227948222 ...
Konchog's user avatar
  • 1,942
0 votes
1 answer
103 views

Why does DispatchQueue.sync cause Data race?

Based on my printed output in the console window, the work in que2 was only executed after the que1 fully finished its work, so my question is why did I get the Data race warning even though the first ...
Steven-Carrot's user avatar
1 vote
1 answer
1k views

DispatchQueue.global() default qos is userInitiated?

I write a demo let queue = DispatchQueue.global() queue.async { let group = DispatchGroup() group.enter() DispatchQueue.global().asyncAfter(deadline: DispatchTime.now(), qos: .default) { ...
frank's user avatar
  • 2,347
0 votes
1 answer
537 views

DispatchGroup notify before all leave called

I have an iOS application that runs multiple classes(every class in a separate thread) and I want to get a notification when all the class finishes their run. So I have a BaseClass: class BaseClass { ...
YosiFZ's user avatar
  • 7,812
0 votes
2 answers
318 views

Alternative ways to receive result from async/await on Main Thread for Swift

Recently, I ran into a problem where I got a warning for updating UI on background task. func didInit() async { listOfTodo = await interactor.getTodos() } I tried to wrap the function body inside ...
Steven-Carrot's user avatar
-2 votes
2 answers
206 views

Parallelism within concurrentPerform closure

I am looking to implement concurrency inside part of my app in order to speed up processing. The input array can be a large array, that I need to check multiple things related to it. This would be ...
mikemgg123's user avatar
0 votes
1 answer
102 views

Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread."

I have a crash on my app " Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread ...
amify's user avatar
  • 1
2 votes
0 answers
190 views

DispatchSemaphore + DispatchQueue not working as expected?

I'm trying to improve the time it takes for a task to finish by leveraging multithreading/paralleling. I'm reading CMSampleBuffers from a video track, manipulating them, then storing them back to an ...
Roi Mulia's user avatar
  • 5,696
1 vote
0 answers
170 views

Task with high quality-of-service submitted to low QoS queue

I'm learning Swift concurrency and encountered this excerpt “If you submit a task with a higher quality of service than the queue has, the queue’s level will increase. Not only that, but all the ...
chlkdst's user avatar
  • 175

1
2 3 4 5
76