dispatch-async

Why does it take such a long time for UI to be updated from background thread?

被刻印的时光 ゝ 提交于 2019-11-27 06:28:05
问题 I understand that all UI updates must be done from Main thread. But purely for the sake of deeper understanding how GCD and dispatch main work: I have a button that runs a network call and in its completionHandler I eventually do: self.layer.borderColor = UIColor(red: 255/255.0, green: 59/255.0, blue: 48/255.0, alpha: 1.0).cgColor self.layer.borderWidth = 3.0 For the color change to happen it takes 6-7 seconds. Obviously if run the above code from main thread it would change the border color

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

孤街醉人 提交于 2019-11-26 23:50:54
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 UPDATE 1 // DATA PROCESSING 2 // UI UPDATE 2 // DATA PROCESSING 3 // UI UPDATE 3 } As it is time consuming I wanted to do the data processing on the background thread, using, dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{ But as both data processing and UI updates are in the same method, I wanted to move only the UI updates

iPhone - Grand Central Dispatch main thread

ぐ巨炮叔叔 提交于 2019-11-26 13:59:14
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 even dispatch_sync(dispatch_get_main_queue(), ^{ ... do stuff I mean, in both cases you are firing a block to be executed on the main thread, exactly where the app runs and this will not help to reduce the load. In the first case you don't have any control when the block will run. I have seen cases of blocks being executed half a second after you fire them. The second case, it is similar to

Long cycle blocks application

巧了我就是萌 提交于 2019-11-26 12:33:13
问题 I hve following cycle in my app var maxIterations: Int = 0 func calculatePoint(cn: Complex) -> Int { let threshold: Double = 2 var z: Complex = .init(re: 0, im: 0) var z2: Complex = .init(re: 0, im: 0) var iteration: Int = 0 repeat { z2 = self.pow2ForComplex(cn: z) z.re = z2.re + cn.re z.im = z2.im + cn.im iteration += 1 } while self.absForComplex(cn: z) <= threshold && iteration < self.maxIterations return iteration } and rainbow wheel is showing during the cycle execution. How I can manage

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

淺唱寂寞╮ 提交于 2019-11-26 08:48:31
问题 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 UPDATE 1 // DATA PROCESSING 2 // UI UPDATE 2 // DATA PROCESSING 3 // UI UPDATE 3 } As it is time consuming I wanted to do the data processing on the background thread, using, dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

iPhone - Grand Central Dispatch main thread

青春壹個敷衍的年華 提交于 2019-11-26 03:47:08
问题 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 even dispatch_sync(dispatch_get_main_queue(), ^{ ... do stuff I mean, in both cases you are firing a block to be executed on the main thread, exactly where the app runs and this will not help to reduce the load. In the first case you don\'t have any control when the block will run. I have

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

☆樱花仙子☆ 提交于 2019-11-26 03:19:10
问题 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 = self.loadOrGenerateAnImage() // Bounce back to the main thread to update the UI dispatch_async(dispatch_get_main_queue()) { self.imageView.image = image } } Or stuff like this to delay execution: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC

Understanding dispatch_async

喜欢而已 提交于 2019-11-26 00:49:36
问题 I have question around this code dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; }); The first parameter of this code is dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) Are we asking this code to perform serial tasks on global queue whose definition itself is that it returns global