Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of block Circular reference in Objective-C

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the analysis of block circular citation in Objective-C". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "block loop citation problem analysis in Objective-C"!

Goal: self will not be released during block execution; it can be released after execution.

At first

The direct use of self in block will strongly reference.

Self.myBlock = ^ () {[self doSomething];}

Or use the properties of the object

Self.myBlock = ^ () {NSString * str = _ str; NSString * str2 = self.str;}

In such cases, the self strong reference block,block also holds the object, resulting in a circular reference.

It is important to note that this problem occurs only when self strongly references block. There is no circular reference to the inline block that is generally used when using GCD or NSOperation.

Join weak self

_ _ weak _ typeof (self) weakSelf = self;self.myBlock = ^ () {[weakSelf doSomething];}

In this way, self holds block, but block is a weak reference to self, which does not result in circular references.

In the [weakSelf doSomething] process, self will not be released, perfect.

But what if that's the case?

_ _ weak _ typeof (self) weakSelf = self;self.myBlock = ^ () {[weakSelf doSomething]; [weakSelf doSomething2];}

Between [weakSelf doSomething] and [weakSelf doSomething2], self may be released. This can lead to strange problems.

Join strong self

_ weak _ typeof (self) weakSelf = self;self.myBlock = ^ () {_ strong _ typeof (self) strongSelf = weakSelf; [strongSelf doSomething]; [strongSelf doSomething2];}

In this way, block not only does not hold the self, but also ensures that the self will not be released during the execution of the block, which really achieves the original goal.

At this point, I believe you have a deeper understanding of "block loop citation problem analysis in Objective-C". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report