Madu
Madu

Reputation: 5019

Weak Pointer is nil in block

I am creating a weakself just right above my block

 __weak typeof (self) weakSelf = self;
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10.0]
                                       queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError ){
                                           NSArray *array = weakSelf.myArray;
                                           NSError *error;

}

In side the block the weakself is nil and every property related to weakself is also nil.

Can some tell me what i am doing wrong here?

Upvotes: 0

Views: 234

Answers (1)

Wain
Wain

Reputation: 119031

There is nothing 'wrong' as such. The object pointed to by self has been deallocated (and thus the reference is nil).

Remember that the proximity of 2 pieces of code does not equate to the proximity of the time at which they will run when you are using asynchronous callbacks.

You need to check why the instance is being deallocated when you don't expect it to be (you could implement dealloc and add a breakpoint there).

Upvotes: 4

Related Questions