Discussion on the use of IOS __weak and __block

Source: Internet
Author: User

In the basic development of the need for weak references when I generally use weak to prevent deadlocks when I will use block

It's not really a big mistake, but it's a bit of a big deal. Now I want to understand these two modifiers

"The external variables used in the block are best used for __weak modification to avoid memory leaks; The root cause of the block's easy reference loop is: 1, for external variables (used inside blocks), the time to execute retain is out of sync with the block's execution time, and the external variable is retain at the time of the block declaration. When the block is executed or even executed is unpredictable; 2,block is generally anonymous and copy-assigned, manually releasing block objects is difficult "

I understand that it is suggested that when using closures, it is recommended that external variables be decorated with __weak weak references with the following two points:

1. If the object referenced by the variable with the __weak modifier is discarded, the nil is assigned to the variable

The code below can be interpreted as nil Helperview when Self.helperview is discarded.

Self.helperview = [[Littlehelperview alloc]init];//__weak helperview objects referenced self.helperview        * Helperview = self.helperview;//__weak modified  variable Helperview
        = ^ (Littlehelperactiontype actiontype,__weak helperlistaction * _nonnull  helperaction) {            [self Golittlehelperviewaction:actiontype andaction:helperaction];        };

2. use a variable with the __weak modifier, that is, use the object registered with the Autoreleasepool

My understanding is that the operation of __weak Littlehelperview *helperview is the operation of Self.helperview means that the operation of weak reference is the indirect operation of weak reference objects

Since the object that was first generated and held is a strong reference held with the __strong modifier variable self.helperview, it always exists until the scope of the variable is closed. So, as shown below, the object referenced by the variable Helperview with the __weak modifier can be persisted until the scope of the variable is ended.

Actually, these two articles are about a meaning.

Online an article is very useful to share to everyone http://my.oschina.net/u/255456/blog/423991

__block The advantage of using the __weak modifier is that you can change the value of the variable __weak just reference __block to modify the value of the Reference object

That is, in block usage, the value of the external local variable can be read inside the block. But when we need to change the value of this variable, we need to append the modifier to it __block .

__blockAnother more usage scenario is that, to avoid the problem of block circular referencing in some cases, we can also add the __block modifier to the corresponding object

That's why I said the orange one above.

Reference http://chun.tips/blog/2014/11/13/hei-mu-bei-hou-de-blockxiu-shi-fu/

Use scene emulation __block to avoid circular references

If you are referencing an instance variable, it retain directly to self, which can sometimes result in a reference ring (two or more objects referencing each other directly or indirectly) and causing a memory leak. The workaround is to create a pointer to self when you need to access the instance variable in the block, and use the __block modifier for it, so that it is not automatically retain: -(void) foo {    = self ;     ^ {        blockself3;    }}

Reference http://m.blog.csdn.net/blog/shishuio/8468423

Discussion on the use of IOS __weak and __block

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.