Questions tagged [dispatch-async]

437 questions
263
votes
6 answers

How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?

I have lots of code in Swift 2.x (or even 1.x) projects that looks like this: // Move to a background thread to do some long running work dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let image =…
rickster
  • 124,678
  • 26
  • 272
  • 326
245
votes
3 answers

Understanding dispatch_async

I have question around this code dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; [self…
user2332873
  • 2,569
  • 3
  • 13
  • 7
146
votes
6 answers

iPhone - Grand Central Dispatch main thread

I have been using with success, grand central dispatch in my apps, but I was wondering what is the real advantage of using something like this: dispatch_async(dispatch_get_main_queue(), ^{ ... do stuff or…
Duck
  • 34,902
  • 47
  • 248
  • 470
49
votes
8 answers

Does dispatch_async(dispatch_get_main_queue(), ^{...}); wait until done?

I have a scenario in my app, where I want to do some time consuming task which consists of some data processing as well as UI update, in a method. My method looks like this, - (void)doCalculationsAndUpdateUIs { // DATA PROCESSING 1 // UI…
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
34
votes
4 answers

Swift 3 warning for dispatch async

I have this code: DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async { let url = URL(string: itemImageURL ) let data = try? Data(contentsOf: url!) if data != nil { …
asheyla
  • 3,175
  • 5
  • 18
  • 34
33
votes
3 answers

dispatch_async and block in iOS

What this piece of code mean? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ TMBaseParser *parser=[[TMBaseParser alloc] init]; parser.delegate=self; NSString *post =nil; NSData…
Tunvir Rahman Tusher
  • 6,421
  • 2
  • 37
  • 32
20
votes
4 answers

DispatchQueue crashing with main.sync in Swift

Please explain to me why I am getting this crash? Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) in this DispatchQueue.main.sync { print("sync") } This is my code. override func viewDidLoad() { …
Sankalap Yaduraj Singh
  • 1,167
  • 2
  • 14
  • 32
17
votes
3 answers

Does using dispatch_get_main_queue() mean that my code will be on the main thread?

Does the following code run on the main thread? Does "main queue" refer to the main thread? dispatch_async(dispatch_get_main_queue(), ^{ // Some code });
aryaxt
  • 76,198
  • 92
  • 293
  • 442
16
votes
2 answers

Swift Async print order?

Does this always print in the order of 1 5 2 4 3? print("1") DispatchQueue.main.async { print("2") DispatchQueue.main.async { print(3) } print("4") } print("5") I feel the answer is no, but I cannot explain it and hope…
Unikorn
  • 1,140
  • 1
  • 13
  • 27
15
votes
3 answers

Objective C - Unit testing dispatch_async block?

I read other posts that come up with solutions to this question. However, their solutions require hacky code to be added to my application in order to be able to test it. To me clean code is more important than unit test. I use dispatch_async in my…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
13
votes
2 answers

synchronized blocks and dispatch_async

What happens to the lock in IOS using @synchronized() when we call dispatch_async() within the block. For ex: id myID -(void) foobar { @synchronized(myID){ dispatch_async(){ //do stuff with myID}; } } Is the lock still valid…
12
votes
1 answer

Crashed: com.apple.root.default-qos

I have a fairly simple app that parses a RSS feed and shows it's content in a table view. It's available on the App Store. I have Crashlytics crash reporting integrated. I recently received two reports. These are a little difficult to decipher. This…
Isuru
  • 30,617
  • 60
  • 187
  • 303
10
votes
2 answers

GCD dispatch_async memory leak?

The following code will occupy ~410MB of memory and will not release it again. (The version using dispatch_sync instead of dispatch_async will require ~8MB memory) I would expect a spike of high memory usage but it should go down again... Where is…
10
votes
1 answer

How to convert (OS_dispatch_data *) 5018112 bytes into NSData to put into UIImage

I've been looking for an answer to this and some seem like they might be what I need but I'm not sure. I found this Question #9152851 and this Question #2617625 and poked around on a bunch of links but I need some direction here. Essentially, I'm…
Patricia
  • 5,019
  • 14
  • 72
  • 152
9
votes
2 answers

dispatch_async vs dispatch_sync execution order

I have a serial dispatch queue created with: dispatch_queue_t serialQueue = dispatch_queue_create("com.unique.name.queue", DISPATCH_QUEUE_SERIAL); I want to use this serial queue to ensure thread safety for class access, while automatically doing…
1
2 3
29 30