Frage

In the Apple Docs, it says:

It’s best practice to use only one block argument to a method.

Is it ok to pass multiple block arguments; or should this be avoided?

Specifically here are a few options I'm working with (I'm hoping to use the first one):

-(void) doSomethingWithSuccessBlock:(void(^)(void))successBlock withFailureBlock:(void (^)(NSError *)) failureBlock

OR

-(BOOL) didDoSomethingWithFailureBlock:(void (^)(NSError *)) failureBlock

OR

-(void) doSomethingWithCallbackBlock:(void (^)(BOOL, NSError *)) callbackBlock
War es hilfreich?

Lösung

Using more than one block as a parameter will warp your mind, curve your spine, and make the enemy win the war. (to paraphrase George Carlin)

Kidding aside, it's a good goal to only have 1 block parameter, but as others have pointed out, Apple has a number of classes who's methods take multiple blocks. Before designing an API that uses multiple block parameters, spend some time thinking about alternatives, and pondering what it does to readability and maintainability of your code. Is there a simpler way to accomplish the same goal? If multiple blocks is the cleanest way to accomplish your goal, then use it.

I think your example of a method that takes both a success block and a failure block is pretty reasonable. You could refactor it into a single block that takes a BOOL parameter "success" and interrogate that parameter in the block to decide what to do. (the CAAnimation delegate method animationDidStop:finished: uses this approach. Actually, so does the completion block in UIView animation, come to think of it.)

You'll have to decide if that makes the method and the blocks you pass to it simpler and cleaner or more complex.

Andere Tipps

Yes; the success/failure block semantic is already being used by AFNetworking, so it's OK for sure.

As iOS SDK uses several block parameters in method then it's okey to implement your custom code in such way.

iOS standard method with 2 block parameters example:

[UIView animateWithDuration:animationDuration animations:^{

} completion:^(BOOL finished) {

}];

I don't mind multiple blocks; however, using multiple blocks like this tends to make me extremely frustrated. Some will disagree, but if I'm sending an operation, I don't want two separate answers, I want one answer with the information I need. Don't send success/error, just include an error value if there was an error. With that said, for things like animation and completion, there are cases where it's beneficial.

I'd really prefer dealing with something like this:

-(void) doSomethingWithCompletionBlock:(void(^)(NSError *))completionBlock;

Then:

[self doSomethingWithCompletionBLock:^(NSError * error) {

    if (!error) {
        // Success  
    }
    else {
        // Failure 
    } 

}];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top