Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I prefer to use a generic and simplistic completion block for asynchronous <em>methods</em> when possible and when suitable as in your case. There is no special "failure" block - since the failure case will be handled by the call-site directly in the completion block. See example below:</p> <pre><code>typedef void (^completion_t)(id result); -(void) loginUser:(NSString *)username withPassword:(NSString *)password completion:(completion_t)completionHandler; </code></pre> <p>(see below for a better naming of the method)</p> <p>In the documentation you state:</p> <blockquote> <p>The result parameter returns @"OK" in case of success, otherwise a NSError object containing details about the error.</p> </blockquote> <p>(Of course, you may return something different, and more suitable as the result in case of success)</p> <p>Well, then you may use it as follows:</p> <pre><code>[foo loginUser:user withPassword:pass completion:^(id result){ if (![result isKindOfClass:[NSError class]]) { ... } else { NSLog(@"Error: %@", error); } }]; </code></pre> <hr> <p>There are countless other valid and reasonable signatures for a completion handler:</p> <pre><code>typedef void (^completion_t)(NSData* data, NSError* error); </code></pre> <p>This one, apparently, returns a NSData in case of success, and a NSError in case of a failure. The documentation <em>shall</em> specify the details.</p> <hr> <p>You may define different handlers (blocks) as <em>properties</em> when you have an <em>Operation</em> object which performs an <em>asynchronous</em> task (e.g., a subclass of <code>NSOperation</code>) Since your example deals with an asynchronous <em>method</em>, there's no reason to explain that here, unless you post a new question. ;)</p> <p>Edit:</p> <p>According the naming conventions, I would suggest the following:</p> <pre><code>-(void) loginWithUsername:(NSString *)username password:(NSString *)password completion:(completion_t)completionHandler; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload