Re: Cancelling dispatch_after?


Quincey Morris
 

On Nov 8, 2017, at 16:37 , Jens Alfke <jens@...> wrote:

ARC copies the block automatically if you assign it to a variable instead of just having it as a parameter of a function/method call

I think that should be an “or”: if you assign it to a non-local variable *or* pass it as a parameter. That’s because the block is implicitly moved to the heap (which is what a copy does) if the reference to it can escape the current scope:


under the heading “Objects Use Properties to Keep Track of Blocks”.

I think you do have a potential problem, though, if you’re trying to cancel pending invocations when posting a new invocation. If you have an instance property or variable to keep track of the pending block, the logic for cancelling it is not automatically thread safe. (More specifically, the plain logic for testing whether it is still pending is not thread safe.) You’re going to have to figure out a way to make it thread safe. Of course, if you're dispatching these blocks to the main queue, from code executing in the main thread, it’s automatically safe.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.