Tag: objective c blocks

Returning block that lives on the local stack

The clang analyzer can check for returning stack-based memory. dispatch_block_t getPrintBlock (const char *msg) { return ^{ printf(“%s”, msg); }; } raises this error: returning block that lives on the local stack What does this error mean?

autorelease]] crashes on dealloc'ing the array

I wrote a class for declaratively describing a sequence of UIView animations. My method takes a vararg of animation blocks and puts them in an array. So in my loop I want to do this: [animations addObject:[[block copy] autorelease]]; I first copy the block so that it is moved to the heap, allowing it to […]

Block automatic retaining, does it affect even to ivars in self?

If I have class: @interface A : NSObject { BOOL b; id c; } @end and reference b and c in a block, is the block retain self automatically? Or just b and c? About c, it may be retained itself, but how about b?

Performing block operations on objects in array and completing when all complete

I have an array of objects on which I would like to perform block operations. I am not sure the best way to do this. I am doing something like in the code below but I don’t think this is the best practice. What is the best way to do such an operation? – (void)performBlockOnAllObjects:(NSArray*)objects […]

static variables inside Objective-C blocks?

I’m working on some CoreAnimation stuff. A navigation controller with a few view controllers. And the view controllers have UISCrollViews for different “pages” of a “brochure.” On each page, there might be some animation that gets triggered when the user flips to that page. I was trying something like this (for one-shot animations). void (^animationBlock)() […]

Objective C : class init with block ?

Is it possible to, say, use a block as a completion handler in a View Controller’s init method so that the parent view controller is able to fill in the details in a block without having to create a custom initWithNibName:andResourceBundle:andThis:andThat: for each possible properties ? // … in the didSelectRowAtIndexPath method of the main […]

How to make a function call in ios to wait, till the block inside that function is executed completely?

Inside the following function, I’ve used a block. But when I call this function, it is returned even before the block is executed. I understood that Block inturn uses the threads and executes separately so that the function doesnt wait for it to return. But, Is there any other way I could make the function […]

beginSheet: block alternative with ARC?

Mike Ash created an example of using blocks to handle callbacks from sheets, which seems very nice. This was in turn updated to work with garbage collection by user Enchilada in another SO question at beginSheet: block alternative?, see below. @implementation NSApplication (SheetAdditions) – (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow didEndBlock:(void (^)(NSInteger returnCode))block { [self beginSheet:sheet modalForWindow:docWindow […]

Is it possible to define a block as a member of a class?

I’m trying to implement a very simple strategy class in Objective-C that allows for strategies to be defined inline instead of being defined through inheritance. Currently my code looks like this: @interface SSTaskStrategy : NSObject { (NSArray *)(^strategy)(void); } @end I thought this would work, but I’m getting the error Expected specifier-qualifier-list before ‘(‘ token […]

In which thread are iOS completion handler blocks called?

For example, in GKScore‘s reportScoreWithCompletionHandler (documentation), suppose you call [score reportScoreWithCompletionHandler:^(NSError *error) { // do some stuff that may be thread-unsafe }]; In which thread will the completion handler be called: the main thread, the same thread as reportScoreWithCompletionHandler was called, or a different thread (presumably the thread that the actual score reporting is done)? […]