Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Question regarding @weakify and @strongify #65

Closed
ghost opened this issue Apr 3, 2014 · 3 comments
Closed

Question regarding @weakify and @strongify #65

ghost opened this issue Apr 3, 2014 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 3, 2014

I've just started using the @weakify and @strongify macros from EXTScope.h and have a quick question about their proper usage.

If used with a method such as UIView's animateWithDuration:animations:completion: do I need to use @strongify in each block I reference the variable passed to @weakify?

For instance, is this the correct usage:

NSTimeInterval duration = 0.125;
@weakify(self);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    @strongify(self);
    [self.flashView setAlpha:1.0f];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        @strongify(self);
        [self.flashView setAlpha:0.0f];
    } completion:^(BOOL finished) {
        @strongify(self);
        self.flashing = NO;
    }];
}];
@jspahrsummers
Copy link
Owner

Yes, that's correct. Using it within only one of those blocks means you'd still capture self strongly in the others.

However, in this particular case, that won't actually cause a permanent retain cycle. Since animations are transitory in nature, the cycle will eventually be broken when the animation completes naturally (or is interrupted).

I generally don't recommend the weak/strong dance for ephemeral things like animations and network requests.

@ghost
Copy link
Author

ghost commented Apr 3, 2014

Okay, cool. Thanks @jspahrsummers

Appreciate your comment about animation blocks and the like; I'd not actually considered that 👍

@ghost ghost closed this as completed Apr 3, 2014
@jspahrsummers
Copy link
Owner

No problem! ✌️

diederich added a commit to cocologics/libextobjc that referenced this issue Jun 28, 2017
Submodule changes to be committed:

* Configuration c8e5527...4ac967d (12):
  > Merge pull request jspahrsummers#80 from cocologics/feature/xcode9-update
  > Merge pull request jspahrsummers#79 from cocologics/feature/xcode9-update
  > Merge pull request jspahrsummers#71 from fabb/analyzer_localized
  > enable CLANG_ANALYZER_NONNULL
  > enable CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION
  > enables SWIFT_TREAT_WARNINGS_AS_ERRORS
  > Merge pull request jspahrsummers#67 from fabb/enable_nullability_completeness_warning
  > Merge pull request jspahrsummers#65 from Abizern/xcode8_debug_flag
  > Merge pull request jspahrsummers#64 from diederich/enableTestability
  > Merge pull request jspahrsummers#63 from diederich/implicitCaptureOfSelf
  > Merge pull request jspahrsummers#62 from calebd/xcode-8-beta-5-warnings
  > Merge pull request jspahrsummers#61 from ikesyo/disable-framework-codesigning
diederich added a commit that referenced this issue Jul 14, 2017
* bumped the xcconfigs to latest version
* fix build on Xcode 9 ('void' missing in strict declaration checking)

Submodule changes to be committed:

* Configuration c8e5527...4ac967d (12):
  > Merge pull request #80 from cocologics/feature/xcode9-update
  > Merge pull request #79 from cocologics/feature/xcode9-update
  > Merge pull request #71 from fabb/analyzer_localized
  > enable CLANG_ANALYZER_NONNULL
  > enable CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION
  > enables SWIFT_TREAT_WARNINGS_AS_ERRORS
  > Merge pull request #67 from fabb/enable_nullability_completeness_warning
  > Merge pull request #65 from Abizern/xcode8_debug_flag
  > Merge pull request #64 from diederich/enableTestability
  > Merge pull request #63 from diederich/implicitCaptureOfSelf
  > Merge pull request #62 from calebd/xcode-8-beta-5-warnings
  > Merge pull request #61 from ikesyo/disable-framework-codesigning
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant