dispatch-async

How does a serial dispatch queue guarantee resource protection?

心不动则不痛 提交于 2019-12-06 09:46:09
//my_serial_queue is a serial_dispatch_queue dispatch_async(my_serial_queue, ^{ //access a shared resource such as a bank account balance [self changeBankAccountBalance]; }); If I submit 100 tasks that each access and mutate a bank account balance, I understand that a serial queue will execute each task sequentially, but do these tasks finish sequentially as well when using dispatch_async? What if task #23 that I submit asynchronously to the serial queue takes a really long time to finish? Would task #24 start only when task #23 is done, or would task #24 begin before task #23 is done? If so,

UIProgressView progress update very slow within AlamoFire (async) call

不问归期 提交于 2019-12-06 09:25:50
问题 Inside an AlamoFire get request I'm trying to update my progress bar. Like so: alamofireManager.request(.GET, urlPath, parameters: params).responseJSON{(request,response,JSON,error) in ...<code here>... dispatch_async(dispatch_get_main_queue(), { NSNotificationCenter.defaultCenter().postNotificationName(LoginVCHideSpinnerAndShowProgressBarName as String, object: self) }) ...<more code here>... } For some reasons this takes a few seconds to execute and if I use dispatch_sync instead the app

Does dispatch_after block the main thread?

梦想与她 提交于 2019-12-05 18:46:44
I'm setting a timer so that after a second passes I reset a value for my keyboard extension. The problem is that I feel like the following call is stalling my UI: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self resetDoubleTapBool]; }) Is there an asynchronous way of doing this, or a better way in general? Thanks! The dispatch_after() call itself does not block. At (or shortly after) the appointed time, the block will be submitted to the main queue. Submitting it doesn't block the main thread. When the main thread next runs its run loop or

Use dispatch_async to analyze an array concurrently in Swift

谁说胖子不能爱 提交于 2019-12-05 02:01:33
问题 I am trying to analyze a photo concurrently using a background thread from GCD. Here is the code I have written: dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) { for (var i = 0; i < 8; i++) { let color = self.photoAnalyzer.analyzeColors(imageStrips[i]) colorList.append(color) } } For clarification on the variable names, here are their descriptions: photoAnalyzer is an instance of a class I wrote called Analyzer that holds all of the methods to process the image.

UIProgressView progress update very slow within AlamoFire (async) call

↘锁芯ラ 提交于 2019-12-04 15:11:04
Inside an AlamoFire get request I'm trying to update my progress bar. Like so: alamofireManager.request(.GET, urlPath, parameters: params).responseJSON{(request,response,JSON,error) in ...<code here>... dispatch_async(dispatch_get_main_queue(), { NSNotificationCenter.defaultCenter().postNotificationName(LoginVCHideSpinnerAndShowProgressBarName as String, object: self) }) ...<more code here>... } For some reasons this takes a few seconds to execute and if I use dispatch_sync instead the app just seems to get stuck at that point but the UI doesn't freeze up (the activity indicator spinner keeps

Swift 3 warning for dispatch async

早过忘川 提交于 2019-12-03 10:34:17
问题 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 { DispatchQueue.main.async{ cell.advImage!.image = UIImage(data: data!) } } } I get this warning in Swift 3: 'default' was deprecated in iOS 8.0: Use qos attributes instead on the first line. Haven't found yet a solution. Has anybody? 回答1: try qos: DispatchQoS.QoSClass.default instead of priority:

Swift 3 warning for dispatch async

帅比萌擦擦* 提交于 2019-12-03 02:06:56
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 { DispatchQueue.main.async{ cell.advImage!.image = UIImage(data: data!) } } } I get this warning in Swift 3: 'default' was deprecated in iOS 8.0: Use qos attributes instead on the first line. Haven't found yet a solution. Has anybody? try qos: DispatchQoS.QoSClass.default instead of priority: DispatchQueue.GlobalQueuePriority.default DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { let url =

dispatch_async UIWebView loadrequest

≯℡__Kan透↙ 提交于 2019-12-02 15:42:50
问题 I'm loading a javascript which is in my App, the script will try to find a solution, which can takes some time. I don't want to wait more than 5 seconds for that solution, in that case I just want to stop the request and show a message to the user. I've been trying to do it with NSTimers and with dispatch_async, but the UIWebView request is still using my whole main queue, and the NSTimer is stopped in the meantime I'm waiting for the answer. The request call: webViewUIView.delegate = self

Passing an argument to dispatch_async

你离开我真会死。 提交于 2019-12-02 13:47:39
问题 I'm new to Swift and looking at how the dispatch_async function works. The API doc shows dispatch_async having two parameters. However, I'm able to pass in one argument and it's okay. dispatch_async(dispatch_get_main_queue()) { } How come I don't need to pass in two arguments? Thank you, API Doc: 回答1: It is a trailing closure syntax func someFunctionThatTakesAClosure(closure: () -> ()) { // function body goes here } // here's how you call this function without using a trailing closure:

NSTimer does not invalidate

岁酱吖の 提交于 2019-12-02 07:25:41
问题 I have a problem invalidating my timer. @property (nonatomic, strong) NSTimer *timer; Inside my block on success, I am allocating and setting my timer on main thread: dispatch_async(dispatch_get_main_queue(), ^{ self.timer = [NSTimer scheduledTimerWithTimeInterval:[Config refreshInterval].integerValue target:self selector:@selector(methodThatHasABlockCalledMentionedAbove) userInfo:nil repeats:NO]; }); At my textFieldDidBeginEditing I am invalidating my timer like this: dispatch_async(dispatch