ios - 我会为 MBProgressHud 使用 block 中的调度吗?
<p><p>在 MBProgressHud 文档中,它声明在调度中使用它,如下所示:</p>
<pre><code>;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
;
});
});
</code></pre>
<p>考虑到您不希望它搞砸主线程,这是完全可以理解的。但是我可以在使用 block 时这样做吗:</p>
<pre><code>;
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error)
{
}
else
{
;
}
}];
</code></pre>
<p>或者我还需要使用 dispatch 吗? </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>把你的代码改成这样:</p>
<pre><code> ;
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
NSLog(@"Am I running on the main thread: %@", ? @"YES": @"NO");
if (error)
{
}
else
{
}
}];
</code></pre>
<p>如果它记录“YES”那么你不需要在主线程上运行<code>;</code>,否则你需要使用</p>
<pre><code>dispatch_async(dispatch_get_main_queue(), ^{
;
});
</code></pre>
<p><strong>更新:</strong></p>
<p> block 在调用它们的任何线程上运行,请注意以下示例:</p>
<pre><code>- (void)viewDidLoad {
;
[self longRunningProcessWithCompletionBlock:^{
NSLog(@"Is running on the main thread? %@", ? @"YES" : @"NO");
}];
}
- (void)longRunningProcessWithCompletionBlock:(void (^)(void))completionBlock {
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this is running on the concurrent thread, so does completionBlock() as it has been called on a concurrent thread.
dispatch_async(concurrentQueue, ^{
;
completionBlock();
});
}
</code></pre>
<p>所以基本上上面的结果将是 <code>"Is running on the main thread? NO"</code></p>
<p>我再次对 <code>viewDidLoad</code> 进行了完全相同的调用:</p>
<pre><code>- (void)viewDidLoad {
;
// Do any additional setup after loading the view, typically from a nib.
[self longRunningProcessWithCompletionBlock:^{
NSLog(@"Is running on the main thread? %@", ? @"YES" : @"NO");
}];
}
</code></pre>
<p>但这一次,我在主线程上调用 <code>longRunningProcessWithCompletionBlock</code> 的 <code>completionBlock</code> 如下:</p>
<pre><code>- (void)longRunningProcessWithCompletionBlock:(void (^)(void))completionBlock {
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
;
//notice the difference, this time we are calling the completionBlock on the main thread, so that block will be running on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock();
});
});
}
</code></pre>
<p>这一次因为我们在主线程上调用了完成 block ,结果会是<code>是在主线程上运行吗?是的</code></p>
<p>简而言之,block 并不能保证它们会在主线程上执行!但它们可以保证它们将在任何调用它们的线程上执行。</p>
<p>在您的情况下,<code>Parse.com</code> 开发人员正在主线程上调用 <code>deleteInBackgroundWithBlock</code> 的完成处理程序 block ,这就是您看到日志为"is"的原因。所以您只需要调用 <code>;</code> 没有 <code>dispatch_async(dispatch_get_main_queue(), ^{ });</code> (因为它已经在主线程,这是一个额外的不必要的步骤)</p></p>
<p style="font-size: 20px;">关于ios - 我会为 MBProgressHud 使用 block 中的调度吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26557320/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26557320/
</a>
</p>
页:
[1]