Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I post a NSNotification when using Grand Central Dispatch?
    primarykey
    data
    text
    <p>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:</p> <pre><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]; } </code></pre> <p>To avoid the UI blocking I have put the writeToFile: into a Grand Central Dispatch queue so it runs as a concurrent thread. But when the write is completed and the thread is done, I want to post an NSNotification. I cannot as the code is shown here because it is in a background thread. But that is the functionality I want to accomplish, realizing this is not workable code:</p> <pre><code>-(void)saveImageToFile { NSString *imagePath = [self photoFilePath]; // execute save to disk as a background thread dispatch_queue_t myQueue = dispatch_queue_create("com.wilddogapps.myqueue", 0); dispatch_async(myQueue, ^{ BOOL jpgData = [UIImageJPEGRepresentation([[self captureManager] stillImage], 0.5) writeToFile:imagePath atomically:YES]; dispatch_async(dispatch_get_main_queue(), ^{ if (jpgData) { [[NSNotificationCenter defaultCenter] postNotificationName:kImageSavedSuccessfully object:self]; } }); }); } </code></pre> <p>What is the correct mechanism here to post this notification to gain the functionality I want ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload