grand-central-dispatch

Grand Central Dispatch (GCD) vs. performSelector - need a better explanation

喜你入骨 提交于 2019-11-26 04:38:10
问题 I\'ve used both GCD and performSelectorOnMainThread:waitUntilDone in my apps, and tend to think of them as interchangeable--that is, performSelectorOnMainThread:waitUntilDone is an Obj-C wrapper to the GCD C syntax. I\'ve been thinking of these two commands as equivalent: dispatch_sync(dispatch_get_main_queue(), ^{ [self doit:YES]; }); [self performSelectorOnMainThread:@selector(doit:) withObject:YES waitUntilDone:YES]; Am I incorrect? That is, is there a difference of the performSelector*

Correct Singleton Pattern Objective C (iOS)?

筅森魡賤 提交于 2019-11-26 04:37:35
问题 I found some information in the net to create a singleton class using GCD. Thats cool because it\'s thread-safe with very low overhead. Sadly I could not find complete solutions but only snippets of the sharedInstance method. So I made my own class using the trial and error method - and et voila - the following came out: @implementation MySingleton // MARK: - // MARK: Singleton Pattern using GCD + (id)allocWithZone:(NSZone *)zone { return [[self sharedInstance] retain]; } - (id)copyWithZone:

GCD to perform task in main thread

拈花ヽ惹草 提交于 2019-11-26 04:33:05
问题 I have a callback which might come from any thread. When I get this callback, then I would like to perform a certain task on the main thread. Do I need to check whether I already am on the main thread - or is there any penalty by not performing this check befora calling the code below? dispatch_async(dispatch_get_main_queue(), ^{ // do work here }); 回答1: No, you do not need to check whether you’re already on the main thread. By dispatching the block to the main queue, you’re just scheduling

What's the difference between performSelectorOnMainThread and dispatch_async on main queue?

不想你离开。 提交于 2019-11-26 04:19:32
问题 I was having problems modifying a view inside a thread. I tried to add a subview but it took around 6 or more seconds to display. I finally got it working, but I don\'t know how exactly. So I was wondering why it worked and what\'s the difference between the following methods: //this worked -added the view instantly dispatch_async(dispatch_get_main_queue(), ^{ //some UI methods ej [view addSubview: otherView]; } //this took around 6 or more seconds to display [viewController

CFRunLoop in Swift Command Line Program

拈花ヽ惹草 提交于 2019-11-26 03:59:44
问题 I am writing a command line application in Swift using a third-party framework that (if I understand the code correctly) relies on GCD callbacks to complete certain actions when a socket receives data. In order to better understand the framework, I have been playing around with a sample Cocoa application the framework\'s author wrote to go along with the framework. Because the sample application is a Cocoa application, the run loops are handled automatically. I\'m including snippets of code

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

Concurrent vs serial queues in GCD

一笑奈何 提交于 2019-11-26 03:46:46
问题 I\'m struggling to fully understand the concurrent and serial queues in GCD. I have some issues and hoping someone can answer me clearly and at the point. I\'m reading that serial queues are created and used in order to execute tasks one after the other. However, what happens if: I create a serial queue I use dispatch_async (on the serial queue I just created) three times to dispatch three blocks A,B,C Will the three blocks be executed: in order A,B,C because the queue is serial OR

Waiting until the task finishes

纵饮孤独 提交于 2019-11-26 03:35:38
问题 How could I make my code wait until the task in DispatchQueue finishes? Does it need any CompletionHandler or something? func myFunction() { var a: Int? DispatchQueue.main.async { var b: Int = 3 a = b } // wait until the task finishes, then print print(a) // - this will contain nil, of course, because it // will execute before the code above } I\'m using Xcode 8.2 and writing in Swift 3. 回答1: Use DispatchGroup s to achieve this. You can either get notified when the group's enter() and leave()

Does ARC support dispatch queues?

拈花ヽ惹草 提交于 2019-11-26 03:32:23
问题 I\'m reading apple\'s documentation about \"Memory Management for Dispatch Queues\": Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does not support the garbage collection model for reclaiming memory. I know that ARC is not a garbage collector but I\'d like to be sure that I don\'t need to dispatch_retain and dispatch_release my dispatch_queue_t 回答1: The short answer: YES, ARC

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