Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling performSelectorInBackground: from background thread

What is the real effect of calling performSelectorInBackground:... from a method that is running in the background? I want it to run asynchronously

For example:

- (void) _imageBufferWasUpdated{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    //do something here

    if(shouldContinue){ 
        [self performSelectorInBackground:@selector(_loop) withObject:nil];
    }
    [pool release];
}

_imageBufferWasUpdated will run in the background and I want to call _loop method asynchronously (in the background also so _imageBufferWasUpdated will finish soon, probably before _loop ends).

Is this correct?

Is there a more efficient (and relatively simple) way to do this using GCD? I would appreciate it if you could give some example on how to fork this using GCD. I think I need at least 3 threads, Main thread, background thread for running _imageBufferWasUpdated and other background thread for _loop. Am I correct?

Thanks in advance Ignacio

like image 553
nacho4d Avatar asked Jul 01 '26 15:07

nacho4d


1 Answers

What you are doing seems fine to me. Cocoa probably uses a single background thread, so it should not lead to excessive thread creation.

If you want more control, you could use NSOperation or GCD. Both are quite simple. Eg, GCD would be like this

#import <dispatch/dispatch.h>

...

dispatch_async( dispatch_get_global_queue(0,0), ^{
    [self _loop];
}];
like image 106
Drew McCormack Avatar answered Jul 04 '26 17:07

Drew McCormack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!