RetainCount OK to use in this case?

RetainCount == BAD

retainCountis taboo, unreliable, unpredictable and should not be used at all. I do not use it anywhere in my code, but I saw it in one class, which I use in an interesting way.

I have a class that starts a thread that runs indefinitely until the thread is canceled. The trick is that the thread increases the number of deductions of the owner, in my case the class that created it. That way, even if I finish using this class, this instance will still depend if someone who runs my class also doesn't know how to close the stream. This is one solution, but this is what I found in the code.

- (oneway void)release
{
    // This override allows allows this object to be dealloced 
    // by shutting down the thread when the thread holds the last reference.
    // Otherwise, the object will never be dealloc'd
    if (self.retainCount == 2)
    {
        [self quitDispatchThread];
    }

    [super release];
}

, , . , 2. , , , ( 2 1), , (quitDispatchThread , ).

...

keepCount, , ?

, retainCount, , . , keepCount - , , , , saveCount - ..

?

, . , . retain release autorelease , , , , .

, .

:: NSThread

, NSThread. - target, selector - , .

initWithTarget: : :

NSThread, .

  • (id) initWithTarget: (id) : (SEL) : (id)

, , .

. .

. .

NSThread, .

, , . .

target . , .

+3
2

retainCount , , .

retainCount IFF, - , Cocoa. , . Cocoa , , , .

, , , .

. , , . , , , , , ( โ€‹โ€‹).

, , . - "shutdown now", ( " " ), . , .

, , ( - , - ). , NSThread initWithTarget:..., / . MVC - "", ( , ) "".

, . . , "", . Voilร , !

+6

. , "Keep Out" .

, . ivar / /.

. release:

. ()

- (oneway void)release

dealloc, 0.

, . ; .

, , .

+2

All Articles