Manual retain with ARC

后端 未结 3 1372
孤独总比滥情好
孤独总比滥情好 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:17

    I have occasionally needed to manually retain and release things (sometimes just for debugging) and came up with the following macros:

    #define AntiARCRetain(...) void *retainedThing = (__bridge_retained void *)__VA_ARGS__; retainedThing = retainedThing
    #define AntiARCRelease(...) void *retainedThing = (__bridge void *) __VA_ARGS__; id unretainedThing = (__bridge_transfer id)retainedThing; unretainedThing = nil
    

    This works by using the __bridge_retained and __bridge_transfer to cast things to and from (void *) which causes things to be retained, or to create a strong reference without calling retain.

    Have fun, but be careful!

    0 讨论(0)
  • 2021-02-01 05:31

    Something like this:

    - (void)startAsyncWork
    {
        id<YourProtocol> delegate = _delegate;
        dispatch_async(/* some queue */, ^{
            // do work
            [delegate doSomething];
        }
    }
    

    The block will retain the delegate as long as needed...

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题