简体   繁体   中英

iPhone SDK Background threads calling other methods

I am a seemingly straightforward question that I can't seem to find an answer to (and it is hindering my app).

I have a background thread running a paricular method:

-(void)processImage:(UIImage *)image {

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  //Process image here in the background here

  [pool drain];
}

This much works great, but my question comes when I want to call another method from inside the already-background method. Does this call stay in the background? Do I need to add NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; and [pool drain]; to the new method to make it run in the background as well?

Any advice would be very helpful. I am a bit confused about this.

Many thanks, Brett

It WILL stay in the background, on the same thread it was called from.

Some threading notes to consider with this:

  • It may not be obvious, but if you call a timer from the background thread, and the thread exits before the timer is supposed to go off, the timer will NOT be called. Thus it is recommended you setup timers from the main thread
  • You dont need another autorelease pool unless you spawn another thread.
  • Any UI updates should be done on the main thread

You don't need to add yet another autorelease pool, the one you already have is enough. And yes, all calls that you make that originate from that thread stay in that thread and thus also run "in the background". Exception would be the use of "performSelectorOnMainThread:", which of course makes the given selector to be performed on the main thread :-) If you want to call GUI methods (like setting the image on an UIImageView) you should make sure to do so on the main thread. See the docs for "performSelectorOnMainThread:waitUntilDone:" (sorry for not giving you you the links, am typing this on my iPad).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM