4

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?

Cœur
  • 32,421
  • 21
  • 173
  • 232
why
  • 21,267
  • 27
  • 91
  • 134
  • 2
    I don't think that code is doing it right actually. Since there's no retaining of the block there's no need to use `weakSelf`. – Carl Veazey Feb 01 '13 at 02:21
  • @CarlVeazey The project is at https://github.com/kolyvan/kxmovie , it works well, but can not work when copy(merge) the code to other project – why Feb 01 '13 at 02:24
  • 4
    Understanding strong vs. weak is a pretty important concept in Objective-C and something that is generally covered very early on in any course/book. If your question is specific to the code ("I understand strong and weak, but I don't understand how it's used in this code"), please rephrase to make that clear. If you're looking for general understanding, I would suggest reading an Objective-C reference since it will be important for all your future code. Also, that question is already covered here: http://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5 – David Ravetti Feb 01 '13 at 02:37

2 Answers2

5

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.

dreamlax
  • 89,489
  • 28
  • 156
  • 207
  • 1
    +1 with a caveat: weak references are *not* set to `nil` if you're using ARC on iOS 4.x or on OS X 10.6. On those systems they behave exactly like `assign`, where you can be left with dangling pointers. – Dave DeLong Feb 18 '13 at 20:00
1

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.

Community
  • 1
  • 1
Ed Chin
  • 1,214
  • 11
  • 24