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.

Grand Central Dispatch (GCD) provides a simple and robust mechanism for concurrent operations in Apple platforms including iOS, macOS, watchOS, and tvOS. This is described in Apple's Introducing Blocks and Grand Central Dispatch:

GCD combines an easy-to-use programming model with highly-efficient system services to radically simplify the code needed to make best use of multiple processors. ...

The central insight of GCD is shifting the responsibility for managing threads and their execution from applications to the operating system. As a result, programmers can write less code to deal with concurrent operations in their applications, and the system can perform more efficiently on single-processor machines, large multiprocessor servers, and everything in between. Without a pervasive approach such as GCD, even the best-written application cannot deliver the best possible performance, because it doesn’t have full insight into everything else happening in the system.

GCD is implemented as a set of extensions to the C language as well as a new API and runtime engine. While initially inspired by the challenge of multicore computing, these actually solve a more general problem: how to efficiently schedule multiple independent chunks of work.

The technology is developed by Apple, and the supporting runtime library is released as open-source.

Alternatives

There are a few native alternatives:

  • Operation queues: Operation queues are built upon GCD technology, but offer more elegant cancelation, priorities, dependencies, and the ability to wrap asynchronous tasks in operations.

  • Combine: The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

  • async-await: Swift concurrency eliminates the completion handler pattern and offers a simpler pattern for representing asynchronous patterns, error handling, etc.

Resources:

3748 questions
1
vote
1 answer

Closure argument passed as @noescape to Objective-C has escaped

I'm trying to use a resource wrapper for a thread safe access. In my implementation, awaitedValue field performs a synchronous read of resource through a serial queue. For some reason, whenever I try to read this property, my app crashes with an…
EBDOKUM
  • 1,696
  • 3
  • 15
  • 33
1
vote
3 answers

why dispatchqueue.global() still alive after deinit

deinit { print("deinit") } DispatchQueue.global().async { [weak self] in while(true){ print(self) print("sleep") sleep(1) } } Although deinit in class is called, infinite loop in…
Young Min Sim
  • 53
  • 1
  • 4
1
vote
2 answers

SwiftUI Global Queue Runs on Thread 1

I am trying to sort a very large array in my mobile app. I am using SwiftUI and would like the array to be sorted once the user taps on a button. This sorting can take about 3 seconds, so I would like this to happen off the main queue. However even…
Matt Bart
  • 809
  • 1
  • 7
  • 26
1
vote
0 answers

App Crashing with libdispatch.dylib _dispatch_lane_resume

I got the logs from Firebase Crashlytics, my app is crashing but doesn't know where it is crashing. As crash logs are also not mentioning any specific line where the app is crashing. Please suggest if anybody else face such type of problem…
1
vote
1 answer

How to Wait for Asynch Firebase Call in Main Thread (Swift)

Background: I am using a library called KolodaView which generates Tinder-esque cards for Swift applications. The Cards are rendered during the viewDidLoad() function of my view controller after assigning the delegate. I wrote the viewForCardAt()…
1
vote
1 answer

Can a Dispatch Semaphore inadvertently deadlock itself?

Say we have a shared resource that a bunch of different global queues have access to and, for the sake of this question, we use a Dispatch Semaphore to manage that access. When one of these global queues tells the semaphore to wait, the semaphore…
lurning too koad
  • 2,698
  • 1
  • 17
  • 47
1
vote
1 answer

Why is the performance gap between GCD, ObjC and Swift so large

OC: Simulator iPhoneSE iOS 13; (60-80 seconds) NSTimeInterval t1 = NSDate.date.timeIntervalSince1970; NSInteger count = 100000; for (NSInteger i = 0; i < count; i++) { …
1
vote
3 answers

In Swift, if Thread.current.isMainThread == false, then is it safe to DispatchQueue.main.sync recursively once?

In Swift, if Thread.current.isMainThread == false, then is it safe to DispatchQueue.main.sync recursively once? The reason I ask is that, in my company's app, we had a crash that turned out to be due to some UI method being called from off the main…
1
vote
1 answer

Understanding deadlock on nested DispatchQueue calls

I have 2 similar cases, first one let queue1 = DispatchQueue(label: "queue1") let queue2 = DispatchQueue(label: "queue2") queue1.sync { print(1, Thread.current) queue2.sync { print(2, Thread.current) } print(3,…
art-of-dreams
  • 241
  • 3
  • 12
1
vote
1 answer

Creating a Grand Central Dispatch queue for a specific singleton

I have a singleton that I use for creating an application wide report. As data is passed to the singleton by the application the singleton then formats the data for use in the report. I use Grand Central Dispatch so that the report creation is not…
Ian Turner
  • 1,363
  • 1
  • 15
  • 27
1
vote
1 answer

Thread Sanitizer in xcode giving wrong error

func doSomething() -> Int { var sum = 0 let increaseWork = DispatchWorkItem { sum = sum + 100 //point 1 } DispatchQueue.global().async(execute:increaseWork) increaseWork.wait() return sum //point 2 } Thread Sanitizer…
1
vote
1 answer

Grand Central Dispatch when program enter suspended mode

Let's assume that I have a GDC thread running when the app goes inti suspended mode. What will happen? Will the tread stop or continue running? Or do I have to stop it myself, in that case how is this done? Thankful for advice!
johan
  • 6,578
  • 6
  • 46
  • 68
1
vote
2 answers

How do you aggregate data from DispatchQueue.concurrentPerform() using GCD?

How is one supposed to aggregate data when using Grand Central Dispatch's ConcurrentPerform()? I am doing what is in the code below, but resultDictionary seems to lose all its data when the notify() block ends. Thus all I get is an empty dictionary…
Stan
  • 51
  • 2
1
vote
2 answers

Can a block in Objective-C take a nil value as a parameter?

The following code that downloads an image and returns the result with a block so that I can take advantage of the async features of blocks and Grand Central Dispatch. I find that if the image or error object is nil I get an EXC_BAD_ACCESS error. Is…
Brennan
  • 11,546
  • 16
  • 64
  • 86
1
vote
0 answers

How can I Call API and Set Maker and Polygon on Google map without freezes?

In my Map Application I am showing almost 1500 area in city using GMUClusterManager. I am calling API and set GSMarker and calling function in DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: { }) in API process start with…
1 2 3
99
100