Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Objective-C blocks vs Java Anonymous Classes or Functions (callbacks)

I'm going to be referring to iOS related Objective-C, as the blocks were just released in version 4, so it's an interesting topic.

Before the blocks functionality, the typical way to do a callback would be to pass the delegate of the object which would be called to the worker object. Once the worker object finished the task, it would then reference a specific method (defined by the protocol), which executes the callback code. This approach works fine, but gets very bloated when you need to do certain kinds of tasks - not going to be getting to the protocols vs blocks in this article though. They do both serve their own purposes, and I'm just going to be getting into the relations they have to the Java anonymous classes.

I'll be the first to say that I'm a huge fan of Java, and anonymous classes are amazing, so the introduction of blocks was a huge relief. Here's an example of how they work:


public class MyClass {
    // this is the callback interface, which you'll implement
    public interface MyClassInterface {
        void doSomething(); 
    }

    public MyClass() { 

    }

    // when you call this, you'll have to implement the interface, and create the 'doSomething' method, which will be called (i.e. callback)
    public void start(MyClassInterface mci) { 
        mci.doSomething();
    }
}

And this is how it's used:


MyClass mc = new MyClass();
mc.start(new MyClass.MyClassInterface() {
    @Override
    public void doSomething() {
        // run some code here
    }
});

By now you must be saying.. well yea, I already knew how to do that, but I want to know how to do that same thing using Objective-C. I'm going to get right to that next - this is where the beauty of blocks comes into play.

I'm going to be keeping all the names the same, so lets begin with the header file:


// we're defining the block here as AnonBlock - you won't need this unless you're using a selector
typedef void (^AnonBlock)();

@interface MyClass {
    
}

@property(readwrite, copy) AnonBlock anonBlock;

- (void) start: (void (^)())block;
// this is if you're using a selector
- (void) callback;

@end

And here is the implementation:


#import "MyClass.h"

@implementation MyClass

@synthesize anonBlock = _anonBlock;

- (void) start: (void (^)())block
{
    // and that's it, we call it just like we did in the java version
    block();
    
    // if you're doing something that requires you to pass a @selector, you need to do it like this - they are commented out, but this should give you an idea of how it works
    // setting the loader and done blocks to the appropriate properties, which will be used in the callback method
    //self.anonBlock = block;

    // starting new managed thread
    //NSOperationQueue *queue = [NSOperationQueue new];
    //NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
    //                                                                        selector:@selector(callback) 
    //                                                                          object:nil];
    //[queue addOperation:operation]; 
    //[operation release];
}

- (void) callback
{
    self.anonBlock();
}

@end

Well that's it - hopefully you found this useful. If you have any questions, please feel free to ask.


jon | August 04, 2011 | Comments (2)

Comments

I do enjoy the manner in which you have framed this particular concern plus it does indeed present me personally a lot of fodder for consideration. Nonetheless, because of everything that I have experienced, I basically trust when the actual feed-back pack on that individuals remain on issue and don’t embark upon a soap box regarding the news of the day. Yet, thank you for this superb piece and even though I can not necessarily go along with this in totality, I regard the perspective.
Comment by carpet cleaning harpenden - November 06, 2011 @ 7:53 pm
Slam dkunin like Shaquille O'Neal, if he wrote informative articles.
Comment by Kory - September 10, 2011 @ 10:57 am

Name (required)
Email (will not be published) (required)
Website

captcha