Should I use the __block specifier on an object pointer, even if it works without it?

I use a completion block CAAnimation(using CAAnimationBlocks ) to provide processing at the end of the animation and part of this completion block changes the animated CALayer. This works even if it is layernot declared using a qualifier __block, since the pointer of the object remains constant, however, I really consider the object as read / write.

One aspect of the Apple Guide that bothers me:

__ Block variables live in a repository that is shared between the lexical region of the variable and all declared blocks and block copies or created within the lexical region of variables.

Given that it layeris a collection iterator that looks like it really will break if I use the specifier __block.

Here is the code in question:

for (CALayer *layer in _myLayers)   // _myLayers is an ivar of the containing object
{
    CAAnimationGroup *group = ...;
    ...
    group.completion = ^(BOOL finished)
    {
        [CATransaction begin];
        [CATransaction setValue:(id)kCFBooleanTrue
                         forKey:kCATransactionDisableActions];
        layer.position = [self _findPosition];
        [CATransaction commit];

        [layer removeAnimationForKey:@"teleportEffect"];
    };

    [layer addAnimation:group forKey:@"teleportEffect"];
}

My real question is: I am doing it right (my spider feeling is tingling).

EDIT I should also add that my application uses MRR, however there is no saving / releasing problem if the layers are static in nature (their lifetime is the content NSView). I also seem to be doing exactly what the Templates, in order to avoid the manual section , say that I should not have done, although it is not clear (to me) why.

+5
source share
2 answers

EDIT:

Anti-patterns, , , - - "-" . for :

void dontDoThis() {
  void (^blockArray[3])(void);  // an array of 3 block references

  for (int i = 0; i < 3; ++i) {
    blockArray[i] = ^{ printf("hello, %d\n", i); };
    // WRONG: The block literal scope is the "for" loop
  }
}
  • blockArray ;

  • for ; - ( ) ; "- " ( ), ;

  • , "-" , for, , ;

  • blockArray;

  • for, blockArray , , , / , , , a for.

, for, .

, -, :

 {
 int array[3];

 for (int i = 0; i < 3; ++i) {
    int a = i;
    array[i] = &a;
    // WRONG: The block literal scope is the "for" loop
 }

, a for , . , a ( ) ( , , C ) , , .

:

__ , , .

, : __block ( ) . , ( ) ( , ), .

, __block , , ARC, , , , ( ARC, __block).

ARC, ARC __block, , . , _myLayers ivar: _myLayers __block, ( ) .

, ARC, , , layer, , . layer _myLayers, , , _myLayers, , . , , , , , . ( , , ).

, .

+1

__block ( ) , - .

, -, layer , _myLayers, , , , , , ... __block , ARC, .

+4

All Articles