Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, I haven't bothered matching your cloud of closing braces, but here's a try by simply using return, which you can use freely inside blocks too and cuts the nesting a little:</p> <pre><code>[self functionA:^(BOOL success) { if (!success) return; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [self functionB:^(NSError *error) { if (!error) return; dispatch_async(dispatch_get_main_queue(), ^(void) { [self functionC:^(id result) { if (!result) return; [self functionD:^(BOOL success) { if (!success) return; [self DoSomething]; }]; }]; }); }]; }); }]; </code></pre> <p>Also, nobody forces you to write blocks <em>inline</em>, you can declare them as normal variables before and use them later. In fact, by declaring blocks before you are able to <em>reuse</em> them if your API is lenient towards its users and allows being called repeatedly even when no work has to be done:</p> <pre><code>- (void)foo:(Bar*)bar { // Prepare the success handler. void (^successBlock)(Bar*) = ^(Bar *bar) { [[NSNotificationCenter defaultCenter] postNotificationName:@"barUpdated" object:bar]; }; if (!bar.didAlreadyFetchStuff) { [self wellYouBetterFetchSomething:bar withSuccess:successBlock]; } else { // Oh, fake we already did the work. successBlock(bar); } } </code></pre> <p>Whenever I see the nest level too high I put the inside blocks as normal methods in the class and simply call them inside the block. The effect is the same, but it looks much cleaner, and it allows you to use appledoc or other documentation tools for each method rather than hoping to understand the mess of nested undocumented blocks.</p> <p>It only gets crazy if you allow it to get crazy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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