简体   繁体   中英

Use dispatch_async() inside a method or when calling that method

I'm working with some code that downloads data. The code is using blocks as callbacks. There are several download methods with very similar code: In the callback block they show a UIAlertView if something goes wrong. The alert view always looks like this:

[req performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    if(error) {
        dispatch_async(dispatch_get_main_queue(), ^{

            [[NSNotificationCenter defaultCenter] postNotificationName:kFailed object:nil];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                         message:@"Connection failed"
                                                        delegate:nil
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles:nil];
            [alertView show];
        });
    }
}];

I want to move the alert view code to a method of its own since it's called several times with the same parameters. Should I move the dispatch_async() to the method too, or should I just wrap calls to that method in dispatch_async() ?

You can do it either way. Functionally these two blocks of code are the same:

Method 1

//.... Assuming this is called in a block
dispatch_async(dispatch_get_main_queue(), ^{
    [self showMyAlertView];
});

- (void) showMyAlertView {
    // Show the alert view and other stuff
}

Method 2

//.... Assuming this is also called in your block
[self showMyAlertView];

- (void) showMyAlertView {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Show the alert view and other stuff
    });
}

Obviously the second way requires the fewest lines of code, but if you want to do other stuff asynchronously (besides show your alert view), you might want to do method 1 so you can add other stuff to the queue.

Hope this helped!

This has nothing to do with wrong or correct.

Advantage: If you place the dispatch_async() inside the method, you can send the message from every place of your program regardless of the thread you are running in.

Disadvantage: If you place the dispatch_async() inside the method, the code is always executed async even the message is sent from the main thread. (In this case dispatch_async() is simply not necessary and a dispatch_sync() would dead lock.)

And vice versa.

To me something different is more important: Define a layer of "dispatch methods". Only use dispatch_async() and dispatch_sync() inside this layer, not in layers built on top of this, not in layers built underneath this.

From higher levels of your software use always this layer. Inside the layer use only methods on a lower layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM