grand-central-dispatch

How do I post a NSNotification when using Grand Central Dispatch?

你。 提交于 2019-12-30 07:11:04
问题 I found that as predicted when I was writing an image to file that my UI was blocked for the duration, which was not acceptable. When I write the image to file I then post an NS Notification so that I can do some other specific jobs related to that completion. Original working but UI blocking code: -(void)saveImageToFile { NSString *imagePath = [self photoFilePath]; BOOL jpgData = [UIImageJPEGRepresentation([[self captureManager] stillImage], 0.5) writeToFile:imagePath atomically:YES]; if

Need to make two HTTP network requests simultaneously (with a completion handler once both finish)

蓝咒 提交于 2019-12-30 01:24:10
问题 I have a situation where I need to make two HTTP GET requests and handle their results only after both are finished. I have a completion handler on each individual network request but it isn't helpful as I don't know when data from both requests are retrieved. I have limited experience with GCD but now that Swift 3 is out, I am trying to figure out how to run multiple tasks and have a single completion handler for them. My research has shown that GCD or NSOperationQueue may be the solution I

checking for equality on dispatch_queue_t

和自甴很熟 提交于 2019-12-29 07:15:12
问题 How can I check for equality between dispatch_queue_t vars? dispatch_queue_t currentQueue = dispatch_get_current_queue(); dispatch_queue_t mainQueue = dispatch_get_main_queue(); if (currentQueue == mainQueue) { } from the docs: typedef struct dispatch_queue_s *dispatch_queue_t; I'm not sure but does this mean that it's a pointer to a dispatch_queue_s struct? Since I can't check equality on pointers, I'm not sure how can I check if a dispatch_queue_t is the same as another? 回答1: This depends

Performance of concurrent code using dispatch_group_async is MUCH slower than single-threaded version

半世苍凉 提交于 2019-12-28 16:14:10
问题 I've been doing some experimentation lately on using large numbers of random numbers to generate "normal distribution" bell curves. The approach is simple: Create an array of integers and zero it out. (I'm using 2001 integers) Repeatedly calculate indexes in this array and index that entry in the array, as follows Loop either 999 or 1000 times. On each iteration: Seed an array index with the center value (1000) Generate a random number = +1/-1. and add it to the array index at the end of the

GCD - main vs background thread for updating a UIImageView

◇◆丶佛笑我妖孽 提交于 2019-12-28 13:45:46
问题 I'm new to GCD and blocks and am easing my way into it. Background: I'm working on a lazy loading routine for a UIScrollView using the ALAssetsLibrary. When my UIScrollView loads I populate it with the aspectRatioThumbnails of my ALAssets and then as the user scrolls, I call the routine below to load the fullScreenImage of the ALAsset that is currently being displayed. It seems to work. (if anyone has a better lazy loading routine please post a comment. I've looked at all I could find plus

GCD - main vs background thread for updating a UIImageView

冷暖自知 提交于 2019-12-28 13:45:32
问题 I'm new to GCD and blocks and am easing my way into it. Background: I'm working on a lazy loading routine for a UIScrollView using the ALAssetsLibrary. When my UIScrollView loads I populate it with the aspectRatioThumbnails of my ALAssets and then as the user scrolls, I call the routine below to load the fullScreenImage of the ALAsset that is currently being displayed. It seems to work. (if anyone has a better lazy loading routine please post a comment. I've looked at all I could find plus

How do I dispatch functions in Swift the right way?

梦想的初衷 提交于 2019-12-28 12:41:05
问题 I've kept trying but I just don't get it. I'm rather new to programming so almost every new step is an experiment. Whereas I have no problems dispatching normal closures without arguments/returns, I haven't understood so far how to deal with functions that take (multiple) arguments and return in the end. To get the logic of the proper "work around" it would be great if someone could post a practical example so I could see whether I've got all of it right. I'd be very thankful for any kind of

Objective-C, cancel a dispatch queue using UI event

99封情书 提交于 2019-12-28 11:55:31
问题 Scenario: User taps a button asking for some kind of modification on address book. A method is called to start this modification and an alert view is shown. In order to show the alert view and keep the UI responsive, I used dispatch_queue: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_sync(dispatch_get_main_queue(), ^{ // Show the alert view }); }); Start the process of address book modification using: dispatch_async(modifyingAddressBookQueue, ^{});

Objective-C, cancel a dispatch queue using UI event

不羁的心 提交于 2019-12-28 11:55:12
问题 Scenario: User taps a button asking for some kind of modification on address book. A method is called to start this modification and an alert view is shown. In order to show the alert view and keep the UI responsive, I used dispatch_queue: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_sync(dispatch_get_main_queue(), ^{ // Show the alert view }); }); Start the process of address book modification using: dispatch_async(modifyingAddressBookQueue, ^{});

Run repeating NSTimer with GCD?

北城以北 提交于 2019-12-28 05:10:16
问题 I was wondering why when you create a repeating timer in a GCD block it doesen't work? This works fine: -(void)viewDidLoad{ [super viewDidLoad]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; } -(void)runTimer{ NSLog(@"hi"); } But this doesent work: dispatch_queue_t myQueue; -(void)viewDidLoad{ [super viewDidLoad]; myQueue = dispatch_queue_create("someDescription", NULL); dispatch_async(myQueue, ^{ [NSTimer