简体   繁体   English

在方法内部或调用该方法时使用dispatch_async()

[英]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. 有几种代码非常相似的下载方法:在回调块中,如果出现问题,它们会显示UIAlertView 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() ? 我是否应该将dispatch_async()移动到方法,或者我应该在dispatch_async()中将调用包装到该方法?

You can do it either way. 你可以这样做。 Functionally these two blocks of code are the same: 功能上这两个代码块是相同的:

Method 1 方法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 方法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. 显然第二种方式需要最少的代码行,但是如果你想异步地做其他事情(除了显示你的警报视图),你可能想要做方法1,这样你就可以将其他东西添加到队列中。

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. 优点:如果将dispatch_async()放在方法中,则可以从程序的每个位置发送消息,而不管您运行的是什么线程。

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. 缺点:如果将dispatch_async()放在方法中,即使从主线程发送消息,代码也始终执行异步。 (In this case dispatch_async() is simply not necessary and a dispatch_sync() would dead lock.) (在这种情况下,dispatch_async()根本不是必需的,dispatch_sync()将死锁。)

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. 仅在此层内使用dispatch_async()和dispatch_sync(),而不是在此基础之上构建的层中,而不是在此下构建的层中。

From higher levels of your software use always this layer. 从较高级别的软件中始终使用此层。 Inside the layer use only methods on a lower layer. 在图层内部仅使用较低图层上的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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