簡體   English   中英

在 ReactiveObjc 中使用 @strongify(self) 之前,我應該檢查弱自我的存在嗎?

[英]Should I check the existence of weakself before use @strongify(self) in ReactiveObjc?

作為weak和strong中的另一種做法,建議我們在加強之前先檢查weakself是否為null。 它看起來像這樣:

__weak weakself = self
someblock {
  if (weakself) {
    __strong self = weakself
    [self doSomeAction];
...
  }
}

我知道 rac 前面做了很多工作,但我想確定我們是否有必要檢查它。 如果沒有,@strongify(self) 如何做到這一點。 謝謝。

不,你應該在strongify之后strongify ,因為它可以停止存在,直到你加強它。

在加強之后它無論如何都不會停止存在,即使檢查nil也不需要任何同步。 我不知道你從哪里得到這樣無效的建議。

if (weakself) { // not nil here
    __strong self = weakself //already nil here
    [self doSomeAction]; //you don't have retain cycles,
    // but there is a potentical crash, for example if you are calling blocks

@strongify 完全按照您已經編寫的方式進行操作,這只是一個方便的宏:

__strong typeof(weakSelf) self = weakself

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM