When designing a block-based API in ObjC, which approach is better, one completion block or two, one each for success and failure?
Let\'s say we have a method retrieving
I generally use the second implementation. A few reasons for it:
It works better for re-use blocks. I often have the same implementation for failures so I can just create a method that returns the failure block and change the success block as necessary. You could do this with the first implementation but you would need nested blocks then.
I like to avoid unnecessary checks when possible ("if error then do something with my error otherwise do something with my object"). Since I will either get an object or an error (never both), I find it inconvenient to pass in both parameters when only one will ever be valid.
I use Restkit and I just need to pass the blocks into my API calls and it will automatically call the success or failure block when the call comes in. If I do the second implementation I need to handle the method callbacks (failure or success method/block) and execute the block manually. I am not sure about AFNetworking though.
I personally think both are fine but in this situation I would argue for the second.