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.
3,697
questions
-1
votes
1
answer
39
views
DispatchGroup sequential execution problem
I am trying to implement dispath group , to perform a sequence of operations , that should execute one after another. But the issue is all the tasks adding to the group are executing in parallel. ...
0
votes
0
answers
30
views
What is @synchronized key in obj-c equivalent of Swift GCD [duplicate]
I need to do some synchronization with @synchronized in Obj-C and also need to sync some code in Swift using GCD.
In @synchronized we pass an object as key but what is its equivalent in GCD? It seems ...
0
votes
1
answer
32
views
Is is possible to mix use Obj-c @synchronized and swift GCD
I need to do some synchronization. One side is written in @synchronized ojb-c. The other side need to be written in Swift.
Is is possible to mix use Obj-c @synchronized and Swift GCD
0
votes
1
answer
53
views
How to use gcd barrier in iOS?
I want to use gcd barrier implement a safe store object. But it not work correctly. The setter sometime is more early than the getter. What's wrong with it?
https://gist.github.com/Terriermon/...
-1
votes
1
answer
58
views
Need to call API's asynchronously and update the UI immediately instead of waiting for all the API to complete in Swift [closed]
I have a PLP page, I need to hit stock API for all the products visible in screen, and Need to update the stock UI for each product once received the response for every product, I have implemented ...
0
votes
1
answer
70
views
Infinite scrolling UITableView with numbers
I need to create infinite table with numbers, that is, when scrolling, new cells with numbers should be created.
I create APICaller with counter, pagination, arrays and while loop.
Also I create ...
1
vote
0
answers
53
views
Infinite loop in pthread_from_mach_thread_np freezing app
I am investigating an issue for an iOS app where the main thread appears to be hung. It believe I have reproduced it locally a couple times, but it is not very repeatable. I have attached a few ...
-2
votes
1
answer
49
views
How to set a variable that runs on background thread and needs to access ui
I am fetching a data source in background. There are 2 urls and I choose it with a tabBar. To know which url I need to access, I use navigationController?.tabBarItem.tag. But It throws an error of &...
-3
votes
2
answers
43
views
why lable's text can't change, but can debug str? [closed]
why lable's text can't change, but can debug str?
i know queue.sync is excute in main thread,when i sleep in main thread。
class ViewController: UIViewController {
let queue = ...
0
votes
0
answers
28
views
Why do images change several times when loading cells?
My application has a directory, it receives data from the JSON RealTime DataBase
and turn it into a model (model has an array of objects, each has a url with an image and a name).
func ...
0
votes
0
answers
90
views
Dispatch queue specific usage for Swift and Objective C inter-op
I am migrating my project from ObjC to Swift. I have this code:
const char *specific = "my_queue"
dispatch_queue_set_specific(myQueue, specific, (void *)specific, NULL);
Later in my unit ...
1
vote
2
answers
95
views
Does GlobalQueue cause memory leaks in iOS?
Here's a very simple demo:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<500000 {
...
0
votes
1
answer
44
views
Delaying a queue for a finite time, conditionally
How could I delay a background queue's execution, without using sleep? Further, how could I interrupt that delay if needs be?
The docs for RunLoop suggest a while loop around the function run with a ...
1
vote
1
answer
53
views
How should I interpret this GCD threading issue message?
The XCODE TSAN thread analyser is throwing up a threading issue:
Data race in generic specialization <Foundation.UUID> of Swift._NativeSet.insertNew(_: __owned τ_0_0, at: Swift._HashTable.Bucket,...
0
votes
1
answer
45
views
SwiftUI toasts not Dismissing
I am using the following method to display a toast message
DispatchQueue.main.async {
Toastmessage = MyToasts(title: "Message Here" , type: .success)
mysettings.showMytoast = true // ...
0
votes
1
answer
195
views
DispatchQueue.sync { } blocks "thread" or "queue"
I'm quite confused.
Code below will cause a deadlock for sure:
// Will execute
DispatchQueue.main.async { // Block 1
// Will execute
DispatchQueue.main.sync { // Block 2
// Will not be ...
0
votes
1
answer
71
views
Swift: How to load results after generateCGImagesAsynchronously is finished?
I am building an ios app that allows you to record a short video which is subsequently split into multiple images, which are in turn classified by a Neural Network. I am using AVAssetImageGenerator's ...
0
votes
2
answers
47
views
Replaying mouse movements with DispatchQueue is laggy
I've recorded a mouse movement (the movement file is here) in the form of (x,y,delay_in_ms) array and I'm trying to replay it back like this:
for movement in movements {
DispatchQueue.global()....
1
vote
1
answer
46
views
dispatchGroup.notify(queue: inheriting QOS from the current context
Is there a way to dispatchGroup.notify(queue: inheriting QOS from the current context? (assuming there IS qos metainfo that's dragged through all the contexts to begin with)
The documentation on ....
0
votes
0
answers
82
views
OperationQueue - crash when editing the same array from multiple operations
I have an OperationQueue with multiple custom Operations which all append to the same array on completion (each operation downloads a file from user's iCloud and when it's done it appends the file to ...
0
votes
0
answers
83
views
Swift 5 Firebase Wait for Async function to be done
I have a loading screen in which(for the challenge) I want to load data in Firebase RealTime Database before accessing to another VC.
I have tried all those methods : https://www.raywenderlich.com/...
0
votes
2
answers
244
views
Where and why deadlock?
I have 2 concurrent queues:
let concurrentQueue = DispatchQueue(label: "test.concurrent", attributes: .concurrent)
let syncQueue = DispatchQueue(label: "test.sync", attributes: ....
1
vote
4
answers
324
views
When to choose serialQueue over concurrent queue in ios
I was asked to implement a thread safe dictionary in swift, I used the common approach:
class MutableDictionary {
var dictionary: [String : Any] = [:]
var queue = DispatchQueue(label: "...
0
votes
0
answers
46
views
Objective C Threading using GCD Without Memory Leak
In iOS and using Objective C and Grand Central Dispatch (GCD) I want to fetch data on a background thread then return with data to the main thread, but am having trouble with memory leaks. Could ...
1
vote
1
answer
65
views
Rx SerialDispatchQueueScheduler doesn't seem to make the code run in serial sequence
I have a problem with an Observable<Data?> function that is called so many times and so fast that the function doesn't complete until the next one is run. This makes sense and is good in most ...
0
votes
0
answers
228
views
Make API call in tableView row and Update particular cell after API response? [duplicate]
I want to make an API call in the tableView cell and Update a particular row after API Response.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
1
vote
1
answer
42
views
DispatchQueue Timeout mechanism not firing / working
I'm running into a bit of a conundrum with the following code snippet. The purpose of this code is to read bytes from an NSInputStream and process them accordingly. However, we have noticed that ...
1
vote
1
answer
211
views
How to call Swift function at specified date/time
What is the best way to trigger a function/block at a precise (within 1 second) date/time in Swift on iOS?
I’ve tried using Timer’s init with fireAt, but I’m finding that the precision is often off by ...
0
votes
2
answers
153
views
How connect DispatchGroup and Executed Request Alamofire?
I have a simple task with threads, but what seems to help everyone else, doesn’t work for me and I don’t understand why.
This is my button processing:
AnyButton(action: {
if ...
1
vote
1
answer
103
views
Electron Node addon with AsyncWorker hang on `dispatch_group_wait` on macOS
I am creating a Node addon to export a video file from macOS Photos library, as it takes a few seconds, I wrapped the code into an AsyncWorker.
The C++ / Objective-C code:
class ...
3
votes
2
answers
434
views
Why concurrent queue with sync act like serial queue?
Could anyone help me to understand this code I created:
let cq = DispatchQueue(label: "downloadQueue", attributes: .concurrent)
cq.sync {
for i in 0..<10 {
sleep(2)
print(i)
}
}
...
2
votes
2
answers
505
views
One method in many Tasks async/await
Hi I have a case where I need to call the same method in multiple Tasks. I want to have a possibility to call this method one by one (sync) not in parallel mode. It looks like that:
var ...
0
votes
2
answers
151
views
Stop function execution until URLSession is completed for recursive api calls
Each API call uses a URLSession shared data task that stalls while the program executes further.
func callAPI(portfolio: Portfolio, symbol: String, endpoint: String){
print("API CALL")
...
0
votes
1
answer
60
views
How to know when iOS app becomes responsive / Hang time
Is there a way to know when an iOS app becomes responsive to user interaction? For example, a user taps a button and the app performs work, this work may dispatch other work asynchronously to the main ...
0
votes
3
answers
193
views
How can I get my app to wait for a permission request to complete?
My updated code is below, added a semaphore, and the app still blows through the AVCaptureDevice.authorizationStatus part and keeps running.
However, if I declare the semaphore with 0, then the first ...
1
vote
1
answer
95
views
DispatchQueue deadlock clarification needed
Look at these two slightly similar cases:
case 1:
func test() {
DispatchQueue.global().sync {
print("a")
DispatchQueue.main.sync {
print("b")
}
}
}
and
...
1
vote
1
answer
33
views
How to sync start of two cyclic tasks (swift)
I have two loops that must run periodically.
One of the loop (A) has a constant duration and is scheduled with high accuracy, no problem.
The other loop (B) can have a variable duration. Much shorter ...
0
votes
1
answer
34
views
Do we use XCTAssertEqual or XCTAssertEqualObject for dispatch_queue_t in Objective C?
I know that dispatch_queue_t is a c struct, and not an NSObject. So should we use XCTAssertEqual or XCTAssertEqualObject? Will it be possible when we have 2 different pointers to the same queue?
1
vote
1
answer
126
views
Can I create a class instance using DispatchQueue.global().async and have its methods run asynchronously?
I created the playground below to answer my question "If I created a class instance using DispatchQueue.global().async would that class remain in its own asynchronous queue? Even if the main app ...
2
votes
1
answer
40
views
How to use `IOConnectCallAsyncScalarMethod` with a GCD dispatch queue
I am working on some code that uses IOConnectCallAsyncScalarMethod() to get callbacks from a DriverKit extension. The setup is quite heavy, involving spawning a thread, manually creating a ...
1
vote
0
answers
62
views
Delegate is not invoked from DispatchQueue.global() thread
I have a scenario where I need to load data from a JSON object, I've created an helper class that does that and it looks like this
protocol JSONDumpHelperDelegate {
func helper(_: JSONDumpHelper, ...
1
vote
1
answer
303
views
Is DispatchQueue using RunLoop?
Every Thread has its own RunLoop, how DispatchQueue interact with them? Is DispatchQueue using RunLoop to dispatch task to Thread or do it by another way?
0
votes
1
answer
224
views
Ask for the Documents permission in a sandboxed app
I am writing a Mac app in SwiftUI and would like to display a live-updated list of documents and folders with the ability to edit the files.
First, users selects any folder with Open File Dialog and ...
0
votes
0
answers
42
views
How to properly deallocate semaphore? [duplicate]
Steps to reproduce crash:
If semaphore.wait() is called and then deinitialize the view controller that owns it before calling semaphore.signal, app crashes.
Getting below error on dinit of the class:
...
1
vote
1
answer
906
views
Is there a way to enforce serial scheduling of async/await calls similar to a GCD serial queue? [duplicate]
Using Swift's new async/await functionality, I want to emulate the scheduling behavior of a serial queue (similar to how one might use a DispatchQueue or OperationQueue in the past).
Simplifying my ...
-1
votes
1
answer
87
views
DispatchQueue.main and DispatchQueue.global are same in swift?
Dispatchqueue.main runs on main thread and global runs on background thread, is that correct? If not, then what’s the difference?
1
vote
1
answer
87
views
how to do blocking `dispatch_sync` using `NSOperationQueue`
Sometimes I need to do synchronous return. In dispatch_sync it's just:
__block int foo
dispatch_sync({
foo = 3
});
return foo;
I am not sure how that translates to NSOperationQueue. I have checked ...
4
votes
1
answer
768
views
Swift 5.5 Concurrency: how to serialize async Tasks to replace an OperationQueue with maxConcurrentOperationCount = 1?
I’m currently migrating my app to use the concurrency model in Swift. I want to serialize Tasks to make sure they are executed one after the other (no paralellism). In my use case, I want to listen to ...
1
vote
0
answers
75
views
GCD dispatch_io_read() intermittent under CPU strain
I'm building a macOS app (Objective-C) and in it I am using dispatch_io_read() to stream live data from a socket and it works great 99% of the time. However, if the computer happens to be under high ...
1
vote
2
answers
252
views
iOS/Swift(UI) Efficient implementation of Background calculation thread always running - GCD case?
To simplify my problem: I have a huge calculation function with 3 inputs and 2 outputs. My input variables are @Published SwiftUI variables inside a SwiftUI ObservableObject. The UI does modify these ...