In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.