在 MBProgressHud 文档中,它声明在调度中使用它,如下所示:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
考虑到您不希望它搞砸主线程,这是完全可以理解的。但是我可以在使用 block 时这样做吗:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error)
{
}
else
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
}];
或者我还需要使用 dispatch 吗?
把你的代码改成这样:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
NSLog(@"Am I running on the main thread: %@", [NSThread isMainThread] ? @"YES": @"NO");
if (error)
{
}
else
{
}
}];
如果它记录“YES”那么你不需要在主线程上运行[MBProgressHUD hideHUDForView:self.view animated:YES];
,否则你需要使用
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
更新:
block 在调用它们的任何线程上运行,请注意以下示例:
- (void)viewDidLoad {
[super viewDidLoad];
[self longRunningProcessWithCompletionBlock:^{
NSLog(@"Is running on the main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
}];
}
- (void)longRunningProcessWithCompletionBlockvoid (^)(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, ^{
[NSThread sleepForTimeInterval:3];
completionBlock();
});
}
所以基本上上面的结果将是 "Is running on the main thread? NO"
我再次对 viewDidLoad
进行了完全相同的调用:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self longRunningProcessWithCompletionBlock:^{
NSLog(@"Is running on the main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
}];
}
但这一次,我在主线程上调用 longRunningProcessWithCompletionBlock
的 completionBlock
如下:
- (void)longRunningProcessWithCompletionBlockvoid (^)(void))completionBlock {
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
[NSThread sleepForTimeInterval:3];
//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();
});
});
}
这一次因为我们在主线程上调用了完成 block ,结果会是是在主线程上运行吗?是的
简而言之,block 并不能保证它们会在主线程上执行!但它们可以保证它们将在任何调用它们的线程上执行。
在您的情况下,Parse.com
开发人员正在主线程上调用 deleteInBackgroundWithBlock
的完成处理程序 block ,这就是您看到日志为"is"的原因。所以您只需要调用 [MBProgressHUD hideHUDForView:self.view animated:YES];
没有 dispatch_async(dispatch_get_main_queue(), ^{ });
(因为它已经在主线程,这是一个额外的不必要的步骤)
关于ios - 我会为 MBProgressHud 使用 block 中的调度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557320/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |