How to cancel NSBlockOperation

Doh. Dear future googlers: of course operation is nil when copied by the block, but it doesn’t have to be copied. It can be qualified with __block like so: //THIS MIGHT LEAK! See the update below. __block NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while( ! [operation isCancelled]){ //do something… } }]; UPDATE: Upon further meditation, it … Read more

What is the difference between a __weak and a __block reference?

From the docs about __block __block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame … Read more

Blocks instead of performSelector:withObject:afterDelay: [duplicate]

There’s no built-in way to do that, but it’s not too bad to add via a category: @implementation NSObject (PerformBlockAfterDelay) – (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay { block = [[block copy] autorelease]; [self performSelector:@selector(fireBlockAfterDelay:) withObject:block afterDelay:delay]; } – (void)fireBlockAfterDelay:(void (^)(void))block { block(); } @end Credit to Mike Ash for the basic implementation.

How to use “enumerateChildNodesWithName” with Swift in SpriteKit?

For now, don’t trust autocomplete to insert the code you need — it drops in signatures from the “header”, but a block signature is not the same as the declaration you need when inserting your own closure for a block parameter. The formal way to write a closure would be to replicate the signature inside … Read more

iOS blocks and strong/weak references to self

You don’t need to make two sets of weak references. What you want to avoid with blocks is a retain cycle—two objects keeping each other alive unnecessarily. If I have an object with this property: @property (strong) void(^completionBlock)(void); and I have this method: – (void)doSomething { self.completionBlock = ^{ [self cleanUp]; }; [self doLongRunningTask]; } … Read more

Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?

The pattern of “run on whatever queue the caller was on” is appealing, but ultimately not a great idea. That queue could be a low priority queue, the main queue, or some other queue with odd properties. My favorite approach to this is to say “the completion block runs on an implementation defined queue with … Read more

How can I retrieve a return value from a completion block?

You’re missing some basics about asynchronous development with blocks. You can’t have a dispatched block return from anywhere but its own scope. Think of each block as its own method, instead of inline code. I think what you’re looking for is something similar to this… – (void)testWithHandler:(void(^)(int result))handler { [obj somemethodwithcompeltionblock:^{ int someInt = 10; … Read more

Block Declaration Syntax List

List of Block Declaration Syntaxes Throughout, let return_type be the type of object/primitive/etc. you’d like to return (commonly void) blockName be the variable name of the block you’re creating var_type be the type object/primitive/etc. you’d like to pass as an argument (leave blank for no parameters) varName be the variable name of the given parameter … Read more

tech