`
lizhuang
  • 浏览: 904228 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

the block would capture a strong reference to self

 
阅读更多
http://blog.csdn.net/likendsl/article/details/37764813
另一种RAC装B写法的解说
http://www.jianshu.com/p/3d6c4416db5e

weak strong dance

常看到一个block要使用self,会处理成在外部声明一个weak变量指向self,在block里又声明一个strong变量指向weakSelf:

__weak __typeof(self)weakSelf = self;
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
    __strong __typeof(weakSelf)strongSelf = weakSelf;
}];


weakSelf是为了block不持有self,避免循环引用,而再声明一个strongSelf是因为一旦进入block执行,就不允许self在这个执行过程中释放。block执行完后这个strongSelf会自动释放,没有循环引用问题。

可以用以下的宏定义
#ifndef    weakify
#if __has_feature(objc_arc)

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
_Pragma("clang diagnostic pop")

#else

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \
_Pragma("clang diagnostic pop")

#endif
#endif

#ifndef    strongify
#if __has_feature(objc_arc)

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __weak_##x##__; \
_Pragma("clang diagnostic pop")

#else

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __block_##x##__; \
_Pragma("clang diagnostic pop")

#endif
#endif
分享到:
评论

相关推荐

    MPC8306 QUICC Engine Block 参考手册

    QUICC Engine Block Reference Manual with ProtocolInterworking Supports: MPC8360E/MPC8358E MPC8568E/MPC8568/MPC8567E/MPC8567 MPC8569E/MPC8306/MPC8306S/MPC8309 MSC8144/MSC8144E/MSC815x Family P1021/P...

    The Block

    标题 "The Block" 提到的是一个可能与设计、排版或者特定字体相关的概念。在IT领域,尤其是在图形设计和用户界面(UI)设计中,“Block”通常指的是布局中的一个单元或区域,它可能包含了文本、图像或其他元素。这种...

    a project model for the FreeBSD Project.7z

    Often used more narrowly in reference to an external deliverable, which is a deliverable that is subject to approval by the project sponsor or customer” by [PMI, 2000]. Examples of outcomes are a ...

    Linux The Journaling Block Device

    Linux The Journaling Block Device

    On the Instability of Bitcoin Without the Block Reward

    it is expected to transition to the latter as the block rewards dwindle. There has been an implicit belief that whether miners are paid by block rewards or transaction fees does not affect the ...

    CUDA Reference from NVIDIA official site.

    `cudaMemset` sets the values of a block of memory on the device to a specified value. This function is useful for initializing device memory before using it in computations. **1.5.9 cudaMemset2D** `...

    block的循环引用导致的内存泄露的示例及解决办法

    1. 使用弱引用(Weak Reference):对于`self`,可以将其在Block内声明为弱引用`__weak typeof(self) weakSelf = self;`,然后在Block内部使用`weakSelf`,避免`self`直接持有Block。 ```objc - (void)viewDidLoad ...

    silence on the wire

    This did not drive me underground (perhaps due to my strong instinct for self-preservation, known in certain circles as cowardice) but somehow provided a good motivation to explore the field of ...

    Ruby.Pocket.Reference

    and the description "Ruby.Pocket.Reference.Jul.2007 learning ruby 的 can reference" indicate that this is a concise guide designed to provide quick access to essential information about the Ruby ...

    power_acdrive.rar_Universal machine_abc to dq_induction motor dq

    The speed control loop uses a proportional-integral controller to produce the quadrature-axis current reference iq* which controls the motor torque. The motor flux is controlled by the direct-axis ...

    The Block chain Developer

    The Block chain Developer

    计算机网络第六版答案

    Hence, it becomes possible for the attacker to issue a command to all the nodes, that target a single node (for example, all nodes in the botnet might be commanded by the attacker to send a TCP SYN ...

    微软内部资料-SQL性能优化2

    A reference to an invalid page (a page that is not in your working set) is referred to as a page fault. Assuming the page reference does not result in an access violation, a page fault can be either ...

    关于block的循环引用

    - 当Block作为类的成员变量时,Block内部引用了`self`,这样`self`就持有Block,Block又持有`self`,形成循环。 - 使用GCD(Grand Central Dispatch)时,若在队列中使用Block操作`self`,未正确处理引用,也可能...

    Blockchain-for-the-Transportation-Industry-Article-0318.pdf

    ew blockchain-based business models are enabled. The system could create an ...the system could access and analyze the information to provide a warning to traffc congestion or inclement weather

    MIBS: A New Lightweight Block Cipher

    In this paper, we propose a new lightweight 64-bit block cipher, which we call MIBS, suitable for resource-constrained devices, such as low-cost RFID tags. We also study its hardware implementation ef...

    iOS Block 界面传值

    - __block_storage_type* ^__strong:栈上的Block,如果离开作用域会被自动销毁。 - id __block __weak:如果Block被复制到堆上,引用会变为弱引用,防止循环引用。 - Block __attribute__((__blocks__(byref)))...

    LTE-Advanced: A Practical Systems Approach to Understanding 3GPP LTE

    and clear and concise descriptions of the underlying principles and theoretical concepts to provide a better understanding of the operation of the system's components. It covers all essential system ...

    微软内部资料-SQL性能优化3

    This removes the requirement to examine every row or page lock on the table to determine whether a transaction can lock the entire table. Lock Mode The code shown in the slide represents how the ...

    block参数,属性,返回值

    当对象持有Block,而Block内部又引用了这个对象时,可能导致对象无法正确释放,这就是所谓的“强引用循环”(strong reference cycle)。为了避免这种情况,可以使用`__weak`关键字弱引用对象,或者在Block内部使用`...

Global site tag (gtag.js) - Google Analytics