ARC __bridge cast Block_copy & Block_release

前端 未结 2 492
遇见更好的自我
遇见更好的自我 2021-01-14 01:43

For some reason I want a block to execute during the next iteration of the run loop, so I came up with:

typedef void (^resizer_t)() ;

- (void) applyResizer:         


        
2条回答
  •  生来不讨喜
    2021-01-14 02:28

    Block are treated as objects so ARC prevent you from casting them to void * without explicit bridged cast. It strange that your compiler doesn't complain on Block_release: it should (on my machine, it does).

    Because ARC treats block as objects, you shouldn't need to use Block_copy nor Block_release anymore. Copy the block (with -[NSObject copy]) when you want it to move to the heap and let the compiler manage the remainder.

    -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:] retains the receiver and the parameter object until the method is invoked. So your block will be retained and released when required. All you have to do is ensure that the block isn't stored on the stack by sending the copy message before passing it to the method.

    Moreover, there is a simpler way to dispatch the execution of a block: it's libdispatch (aka GCD).

    dispatch_async(dispatch_get_main_queue(), resizer);
    

提交回复
热议问题