Using Code Blocks Instead of Delegates with IBM MobileFirst Platform in Native iOS Apps

We’ve been able to write native iOS apps leveraging the scaffolding and analytics of the IBM MobileFirst Platform Foundation Server for a while now. This was first introduced way back when MobileFirst still went by the Worklight name, serveral versions ago.

As I would write apps, one thing I really wanted was to use code blocks instead of having to implement delegate classes every time I need to call a procedure on the MobileFirst server.   In MobileFirst 7.0, the new WLResourceRequest API allows you to invoke requests using either the completionHandler (code block) or delegate implementations.

But… what if you’re still using an earlier version of the MobileFirst platform, or what if you still want to leverage your existing code that uses WLProcedureInvocationData parameters, but don’t want to have to create a new delegate for every request?  Well, look no further.  I put together a very simple utility class that helps with this task by allowing you to pass code blocks as parameters for the requests to the MobileFirst (or Worklight) server.

You can grab the Objective-C client-side utility class from https://github.com/triceam/MobileFirst-Helper

Right now it only contains two utlitiy methods, but I’ll update it if I i come up with anything else useful. 

The invokeProcedure method allows you to invoke a procedure and pass code blocks for success/error callbacks inline, without having to define delegates.

[objc]WLProcedureInvocationData *invocationData =
[[WLProcedureInvocationData alloc]
initWithAdapterName:@"StockAdapter"
procedureName:@"getList"];

[WLClientHelper invokeProcedure:invocationData
successCallback:^(WLResponse *successResponse) {

//handle the response
}
errorCallback:^(WLFailResponse *errorResponse) {

//handle the error response
}];[/objc]

I normally prefer code blocks b/c they allow you to encapsulate functionality inside of a single class, instead of having logic spread between a controller and delegate class (and having to worry about communication between the two).

The other getLoggerForInstance utility function is just a shortcut to get an OClogger instance with the package string matching the class name of the instance passed, with just a single line of code:

[objc]OCLogger *logger = [WLClientHelper getLoggerForInstance:self];[/objc]

Download the utility directly from https://github.com/triceam/MobileFirst-Helper

Enjoy!