Architecting with Blocks part 1

(Note: This is part 1 of a series of posts about Architecting an application using blocks in objective-c. By no means I am an expert on using blocks, and also it’s not the intention of this series to teach you how to use blocks. You can check this for that. To finalise, I am sure there are better ways to use blocks while architecting an app: use this at your own risk. :))

On most of the applications out there ( I would say 60%, not counting with games), a normal application follows the same pattern:

  •  Server Request
  •  Handle Response
  •  Parse it
  •  Show to the user
  • Handle any error that might appear

What I have been using, so far, to handle this typical problem is:

  • ViewController complying to a protocol
  • Pass reference to the Server Requester Class (normally like this @property(weak,nonatomic) id <myProtocol> delegate)
  • Request Data
  • Take care of data (Parsing usually)
  • Using the reference passed before, call the most appropriate method from the protocol (normally either success or failure)

It’s the usual pattern, but it can become a bit verbose, if you happen to have a lot of different UIViewControllers, requesting and expecting different things. So, on my last project I tried to rely mostly on blocks.

I had three different kind of resources to get from the server, one of them would rely on the other two (to make the 3rd request I would need that the user had choose the other two). I started by creating a typedef enum:

// Used to know which request we are using
typedef enum



} kRequest;

You use the typedef enum so you can better understand your code ( kFirstRequest, kSecondRequest and kFinalRequest are nothing more than integrals). Next, you define four blocks:

// Block that will be executed when the server's response has been received and parsed
// In theory, you won't need the typeOfRequest to be passed to the block, but you can use it as well
typedef void(^SuccessBlock)(NSArray *arrayObjects, kRequest typeOfRequest);

// Block that will be executed when we receive the server's response
typedef void(^ParsingBlock)(NSData *serverResponse);

// Block that will be executed if something goes wrong (Request, or Parsing)
typedef void(^ErrorBlock)(NSError *errorObject);

// Block that will be executed if something goes wrong (Request, or Parsing)
typedef void(^RequestBlock)(NSError *errorObject, kRequest typeOfRequest);

This is the base of our code. Instead of using a protocol and pass references between different objects, we will pass blocks. You should add this code to a file called Constants.h. On your prefix.pch file, you should import it. This way, you have access to the blocks and the typedef enum in any part of your application.

Tagged , , , , , , , , ,

One thought on “Architecting with Blocks part 1

  1. […] is the final post from a 2 parts series that I start a long ago. In this case I won’t snippet of code, I will give away an entire sample. My approach about […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: