Do you also consider circular reference issues when using some of the system's block APIs, such as the block version of UIView?

Source: Internet
Author: User
Tags gcd

    • In some block APIs of the system, the block version of UIView is animated when it is written 不需要 ,但也有一些api 需要考虑
    • The following use methods 不会 cause circular reference problems
[UIView animateWithDuration:duration animations:^{ [self.superview layoutIfNeeded]; }];[[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.someProperty = xyz; }];[[NSNotificationCenter defaultCenter] addObserverForName:@"someNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * notification) { self.someProperty = xyz; }];
  • However, if some of the parameters in the method are member variables, you can cause circular references, such as GCD, Nsnotificationcenter calls should be careful, such as GCD internal if self is referenced, and the parameters of the GCD is a member variable, you have to consider the circular reference, for example:

    • GCD

      • parsing: self-->_operationsqueue-->block-->self forms a closed loop, resulting in a circular reference
         __weak __typeof__ (self) weakSelf = Span class= "Hljs-keyword" >self;dispatch_group_async (_operationsgroup, _operationsqueue, ^{[weakSelf doSomething];[ Weakself Dosomethingelse];}
         );
    • Nsnotificationcenter

      • Analysis: self-->_observer-->block-->self form a closed loop, resulting in circular references
      __weak __typeof__(self) weakSelf = self;_observer = [[NSNotificationCenter defaultCenter]addObserverForName:@"testKey"object:nilqueue:nilusingBlock:^(NSNotification *note){ [weakSelf dismissModalViewControllerAnimated:YES];}];

Do you also consider circular reference issues when using some of the system's block APIs, such as the block version of UIView?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.