Manual retain with ARC

后端 未结 3 1377
孤独总比滥情好
孤独总比滥情好 2021-02-01 05:06

Before ARC I had the following code that retains the delegate while an async operation is in progress:

- (void)startAsyncWork
{
    [_delegate retain];
    // ca         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-01 05:33

    Why not just assign your delegate object to a strong ivar for the duration of the asynchronous task?

    Or have a local variable in executeAsyncWork

    - (void)executeAsyncWork
    {
        id localCopy = _delegate;
    
        if (localCopy != nil) // since this method is async, the delegate might have gone
        {
            // do work on local copy
        }
    }
    

提交回复
热议问题