The difference between self.completionBlock = ^ {} and (void) (^ completeBlock) (void) = ^ {}

Recently after the following Apple documentation

Screenshot from an Apple Developer Video I used the following conventions to avoid issues with the save loop.

__weak __typeof(self) weak_self = self;
void(^completionBlock)(void) = ^(){

    __typeof(self) strong_self = weak_self;
    if (strong_self) {

        if (strong_self->_completion != NULL) {
            strong_self->_completion();
        }
    }
};

But this code fails because self gets released before the block is called.

When I used the following, it works.

__block __typeof(self) block_self = self;
void(^completionBlock)(void) = ^(){

     if (block_self->_completion != NULL) {
         block_self->_completion();
     }
};

Now I'm confused when we should use the __weak link. Only in the following case self.completionBlock "

__weak __typeof(self) weak_self = self;
self.completionBlock = ^(){

     if (weak_self->_completion != NULL) {
         weak_self->_completion();
     }
};

Any light under these conditions will be very useful to me.

Below is the code for my implementation.

===================================================

MyViewController File

@implementation MyViewController

//starting point 
- (void)myPushMethod {
    __weak __typeof(self) weak_self = self;
    MyViewControllerTransitioning *delegateObj = [[MyViewControllerTransitioning alloc] initWithCompletion:^{

        //resetting the delegate
        __typeof(self) strong_self = weak_self;
        if (strong_self) {
            strong_self.navigationController.delegate = nil;
        }
    }];
    self.navigationController.delegate = delegateObj;
    [self.navigationController pushViewController:someViewController animated:_animated];
    //it is found that delegateObj is getting deallocated while reaches this point
}

@end

===================================================

MyViewControllerTransitioning File

@implementation MyViewControllerTransitioning

- (instancetype)initWithCompletion:(completionHandler)completionHandler
{
    if(self = [super init])
    {
        if (completionHandler != NULL) {
            _completion  = completionHandler;
        }
    }
    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC
{
    id<UIViewControllerAnimatedTransitioning> animationTransitioning = nil;

    //crashes here
    __block __typeof(self) block_self = self;
    void(^completionBlock)(void) = ^(){

        if (block_self->_completion != NULL) {
            block_self->_completion();
        }
    };

    //showing presentation-up animation if Push
    if (operation == UINavigationControllerOperationPush) {
        animationTransitioning = [[MyAnimator alloc] initWithAnimationCompletion:completionBlock];
    }
    return animationTransitioning;
}

- (void)dealloc{
//dealloc is called before invoking completionBlock
}

@end

===================================================

MyAnimator File

@implementation MyAnimator

- (instancetype)initWithAnimationCompletion:(presentationUpCompletion)completionHandler
{
    if(self = [super init])
    {
        if (completionHandler != NULL) {
            _completion  = completionHandler;
        }
    }
    return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return my_duration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    //animation logic

    [UIView animateWithDuration:duration animations: ^{
        //animation logic
    } completion: ^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)animationEnded:(BOOL) transitionCompleted {
    if (transitionCompleted && _completion != NULL) {
        _completion();
    }
}

@end
+4
2

weakSelf ( weakSelf - strongSelf). , , , weakSelf ( ).

, , , . ( , , , . Lol.) , , . , .

strong ( ), .


.


:

, .

__weak __typeof(self) weak_self = self;

void(^completionBlock)(void) = ^(){
    __typeof(self) strong_self = weak_self;
    if (strong_self) {
        if (strong_self->_completion != NULL) {
            strong_self->_completion();
        }
    }
};

, self .

, ( " " , "). . , .

, (weakSelf strongSelf, weak_self strong_self). __ __typeof. , . , - :

__weak typeof(self) weakSelf = self;

void(^completionBlock)(void) = ^(){
    typeof(self) strongSelf = weakSelf;
    if (strongSelf) {
        if (strongSelf.completion != NULL) {
            strongSelf.completion();
        }
    }
};

, , , . , , , , , " " .

:

__block __typeof(self) block_self = self;

, . " " , ( ), ARC __block , . , , ARC, block_self , ARC.

, :

__weak __typeof(self) weak_self = self;
self.completionBlock = ^(){
     if (weak_self->_completion != NULL) {
         weak_self->_completion();
     }
};

. -, . weak_self nil, . -, , , ivars, , .


, " " . , " " , " . , MCVE, . ARC .

+1

self.completionBlock

self completionBlock ( ), self, .

(void) (^ completeBlock) (void) = ^ {}

self completionBlock ( ), self.

, __block, , "__block ".

+1

All Articles