Objective-C NSThread ref counting convention (save vs autorelease)

My main program creates a thread that does the following:

// alloc autorelease pool somewhere before
NSArray *blah = [NSArray arrayWithObject: @"moo"];
[self performSelectorOnMainThread: @selector(boonk:) withObject: blah
      waitUntilDone: NO];
// release autorelease pool somewhere after

Now this seems bad to me, because the autoplay pool can be released before the boonk: selector is finished, which will lead to a crash.

So my natural next step would be this:

// alloc autorelease pool somewhere before
NSArray *blah = [[NSArray alloc] initWithObject: @"moo"];
[self performSelectorOnMainThread: @selector(boonk:) withObject: blah
      waitUntilDone: NO];
// release autorelease pool somewhere after


- (void)boonk: (id)data
{
   // do something with data
   [data release];   // release the ref count the thread added
}

This is definitely error-free, but .... seems unnatural. Is there an objective-c agreement or reference counting protocol to handle this situation (forwarding a NO-wait stream), or is the second solution higher than how it was made?

+3
source share
2 answers

, performSelectorOnMainThread , , .

+9

; A B, . , , -performSelectorOnMainThread: ( ) , , .

, , --------. , , /.

, , , .

+3

Source: https://habr.com/ru/post/1728023/


All Articles