grand-central-dispatch

Correct Singleton Pattern Objective C (iOS)?

女生的网名这么多〃 提交于 2019-11-26 15:48:43
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:(NSZone *)zone { return self; } - (id)autorelease { return self; } - (oneway void)release { /* Singletons

GCD to perform task in main thread

匆匆过客 提交于 2019-11-26 15:38:46
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 }); 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 the block to be executed serially on the main thread, which happens when the corresponding run loop is run.

Why is this dispatch_sync() call freezing?

血红的双手。 提交于 2019-11-26 15:38:00
问题 I'm using the Kiwi testing framework to test an authentication method in my app. The test freezes at a call to dispatch_sync which looks like this: dispatch_queue_t main = dispatch_get_main_queue(); dispatch_sync(main, ^ { [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAuthenticationSuccess object:nil userInfo:ret]; }); I'd like to know why it freezes there, if anyone has any hints. 回答1: For the second part of your question regarding the hint on the freeze: When

dispatch_sync on main queue hangs in unit test

别等时光非礼了梦想. 提交于 2019-11-26 15:31:38
问题 I was having some trouble unit testing some grand central dispatch code with the built in Xcode unit testing framework, SenTestingKit. I managed to boil my problem done to this. I have a unit test that builds a block and tries to execute it on the main thread. However, the block is never actually executed, so the test hangs because it's a synchronous dispatch. - (void)testSample { dispatch_sync(dispatch_get_main_queue(), ^(void) { NSLog(@"on main thread!"); }); STFail(@"FAIL!"); } What is it

dispatch_get_global_queue vs dispatch_get_main_queue

心不动则不痛 提交于 2019-11-26 15:16:47
问题 Starting to learn about core data and dispatch_async. There is a block of code to get url of image from set of data and set it to model of core data like below dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ NSString *urlString = [[[photoDictionary valueForKey:@"images"] objectAtIndex:0] valueForKey:@"url"]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; dispatch_async(dispatch_get_main_queue(), ^{ [photoModel setValue

Concurrent vs serial queues in GCD

我的梦境 提交于 2019-11-26 14:48:53
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 concurrently (in the same time on parralel threads) because I used ASYNC dispatch I'm reading that I can use

CFRunLoop in Swift Command Line Program

邮差的信 提交于 2019-11-26 14:43:34
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 from the sample application (MIT license) to give an idea of how it works: class AppDelegate: NSObject,

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

Last In-First Out Stack with GCD?

梦想与她 提交于 2019-11-26 12:21:11
问题 I have a UITableView that displays images associated with contacts in each row. In some cases these images are read on first display from the address book contact image, and where there isn\'t one they are an avatar rendered based on stored data. I presently have these images being updated on a background thread using GCD. However, this loads the images in the order they were requested, which means during rapid scrolling the queue becomes lengthy and when the user stops scrolling the current

using dispatch_sync in Grand Central Dispatch

北慕城南 提交于 2019-11-26 11:46:51
问题 Can anyone explain with really clear use cases what the purpose of dispatch_sync in GCD is for? I can\'t understand where and why I would have to use this. Thanks! 回答1: You use it when you want to execute a block and wait for the results. One example of this is the pattern where you're using a dispatch queue instead of locks for synchronization. For example, assume you have a shared NSMutableArray a , with access mediated by dispatch queue q . A background thread might be appending to the