Question

There is the code in one opensource project:

- (id) initWithContentPath: (NSString *) path parameters: (NSDictionary *) parameters
{
    NSAssert(path.length > 0, @"empty path");
    playPath = path;
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _moviePosition = 0;
        _startTime = -1;
        self.wantsFullScreenLayout = YES;

        _decodeDuration = DEFAULT_DECODE_DURATION;
        _minBufferedDuration = LOCAL_BUFFERED_DURATION;

        _parameters = parameters;

        __weak KxMovieViewController *weakSelf = self;

        dispatch_async(dispatch_get_global_queue(0, 0), ^{

            NSError *error;
            KxMovieDecoder *decoder;
            decoder = [KxMovieDecoder movieDecoderWithContentPath:path error:&error];

            NSLog(@"KxMovie load video %@", path);

            __strong KxMovieViewController *strongSelf = weakSelf;
            if (strongSelf) {

                dispatch_sync(dispatch_get_main_queue(), ^{

                    [strongSelf setMovieDecoder:decoder withError:error];                    
                });
            }
        });
    }
    return self;
}

I want to know when one class need to set self to strong or weak?

Was it helpful?

Solution

A strong reference is used when you want to ensure the object you are referencing is not deallocated while you are still using it. A weak reference is used when you don't care if the object you are referencing is deallocated. A weak reference is automatically set to nil when there are no more strong references to that object.

Basically, as long as there is at least one strong reference to an object, it won't be deallocated. When there are no more strong references, all weak references (if any) are set to nil.

OTHER TIPS

If you're looking for a clear explanation of the code you posted, I can try to help with that. I posted a similar question with a hypothesis on what is happening, and waiting on an answer to verify or invalidate my hypothesis.

My question is here: Explain __weak and __strong usage reasons in SDWebImage code

To sum up, here's what I think: This code is initializing an instance of a class. But it needs to run an asynchronous block. The __weak and __strong keywords are used to ensure that the instance is still valid when the asynchronous block is running. If the instance has been deallocated, then there is no need to perform the action "setMovieDecoder".

To answer your specific question, you want to use this code pattern whenever you are running an asynchronous block that needs to update some object instance and you need that instance around to update it. Don't use this code pattern around asynchronous blocks that are simply downloading something such as sync engines.

**** Disclaimer: Check up on answers to my refined question in hopes of getting a real expert explanation of the code pattern I describe above. Hopefully I'm correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top