grand-central-dispatch

GCD dispatch concurrent queue freeze with 'Dispatch Thread Soft Limit Reached: 64' in crash log

℡╲_俬逩灬. 提交于 2019-12-01 02:57:12
问题 My program is a server which handles incoming requests. Each valid request is wrapped in NSOperation and passed to a normal NSOperationQueue . Each NSOpearation processes its request. In some cases, there is contention at a NSDictionary which I use dispatch_queue (concurrent queue), dispatch_barrier_async (when set value) and dispatch_sync (when get value) to make this NSDictionary thread-safe. I test my program with 100 requests concurrently then the process freezes sometimes. I kill the

iphone - is it ok to use usleep on a secondary thread on Grand Central Dispatch?

你。 提交于 2019-12-01 02:01:13
I am adding a block to a queue (not the main queue), using Grand Central Dispatch. This block has a loop and between every passage of the loop a small delay of 0,02 seconds is required. I am using dispatch_async(myOwnQueue, ^{ // begin loop // do stuff usleep(20000); // end loop }); on this queue. As it is not the main queue, it will not block the main thread. The problem is that Xcode complains: warning: implicit declaration of function 'usleep' Is there other way to do that? thanks. You just need to include the appropriate header before calling usleep(): #include <unistd.h> 来源: https:/

Displaying UIAlertView after some time

[亡魂溺海] 提交于 2019-12-01 01:40:08
I'm trying to display a UIAlertView after some time (like 5 minutes after doing something in the app). I'm already notifying the user if the app is closed or in background. But I want to display a UIAlertView while the app is running. I tried to dispatch_async as follows but the alert is popping forever: [NSThread sleepForTimeInterval:minutes]; dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"title!" message:@"message!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; [alert show]; [alert release]; } ); Also, I read that

How to create an array of dispatch_block_t in Swift 2.0?

血红的双手。 提交于 2019-12-01 01:36:14
I have a code in Swift 1.2 to create an array of dispatch_block_t and it works fine. But the same code throws error in Swift 2.0. var menuView: btSimplePopUP! let actions: [dispatch_block_t] = [{self.pickImages()}, {self.takePicture()}, {self.pickVideos()}, {self.shootVideo()}, {self.recordAudio()}, {self.closeView()}] menuView = btSimplePopUP(itemImage: imgs as [AnyObject], andTitles: titles as [AnyObject], andActionArray: actions as NSArray as [AnyObject], addToViewController: self) The above code works fine in Swift 1.2. But in Swift 2.0, it throws the following error [dispatch_block_t] is

How to convert dispatch_data_t to NSData?

江枫思渺然 提交于 2019-12-01 01:09:54
问题 Is this the right way? // convert const void *buffer = NULL; size_t size = 0; dispatch_data_t new_data_file = dispatch_data_create_map(data, &buffer, &size); if(new_data_file){ /* to avoid warning really - since dispatch_data_create_map demands we care about the return arg */} NSData *nsdata = [[NSData alloc] initWithBytes:buffer length:size]; // use the nsdata... code removed for general purpose // clean up [nsdata release]; free(buffer); // warning: passing const void * to parameter of type

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

三世轮回 提交于 2019-11-30 23:43:44
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 (jpgData) { [[NSNotificationCenter defaultCenter] postNotificationName:kImageSavedSuccessfully object:self

iOS GCD Sync with Async Block

為{幸葍}努か 提交于 2019-11-30 23:07:55
I have an async function with a block : [self performAsyncTaskCompletion:(void(^) () ) { //Do Something } ]; I need to call this function many times but in a synchronous way. I tried to use GCD queues : dispatch_queue_t queue = dispatch_queue_create("com.MyApp.task", NULL); for (int i = 0; i < array.count; i++) { dispatch_sync(queue, ^{ [self performAsyncTaskCompletion:(void(^) () ) { //Do Something } ]; }); } But it doesn't work because dispatch_sync is only waiting for the end of the block. How can I ask it to wait for the end of the async functions in its block ? You could use dispatch

How to create an array of dispatch_block_t in Swift 2.0?

拜拜、爱过 提交于 2019-11-30 20:10:10
问题 I have a code in Swift 1.2 to create an array of dispatch_block_t and it works fine. But the same code throws error in Swift 2.0. var menuView: btSimplePopUP! let actions: [dispatch_block_t] = [{self.pickImages()}, {self.takePicture()}, {self.pickVideos()}, {self.shootVideo()}, {self.recordAudio()}, {self.closeView()}] menuView = btSimplePopUP(itemImage: imgs as [AnyObject], andTitles: titles as [AnyObject], andActionArray: actions as NSArray as [AnyObject], addToViewController: self) The

Does NSURLSession Take place in a separate thread?

╄→гoц情女王★ 提交于 2019-11-30 18:58:14
I was designing an app that uses NSURLSession and thinking about putting it in a different thread with Grand Central Dispatch, but if NSURLSession automatically does that in the background, I wouldn't have to use GCD then, correct? So in other words, does NSURLSession automatically use Grand Central Dispatch in the background, so we don't have to worry about it? Yes, NSURLSession does its work in a background thread. The download ALWAYS takes place on a background thread. EDIT: There is no reason to wrap your code that invokes NSURLSession (or URLSession in Swift 3/Swift 4) in a GCD call. You

NSOperationQueue vs GCD

只谈情不闲聊 提交于 2019-11-30 18:19:05
In what cases would you prefer to use NSOperationQueue over GCD? From my limited experience of these two, I take it that with NSOperationQueue you basically have control over how many concurrent operations there are. With GCD you can't do this, since you are using a queue. Except you can somehow simulate this with a multi core processor, although still I think there's no way to control it. NSOperationQueue is built on GCD as of iOS 4. Use the simplest API for the task at hand.Measure if it's a performance problem and then reevaluate if needed. dispatch_async is lower level, usually C-type