// 在主线程中延迟执行某动作,不会卡主主线程,不影响后面的东做执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@", [NSThread currentThread]);
});
// 在子线程中执行某动作,不会卡主整个线程
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
NSLog(@"%@", [NSThread currentThread]);
});
// 实现延迟,该线程本身在哪个线程中就再哪个线程中执行
NSURL *url = [NSURL URLWithString:@"http://59320.jpg.com"];
[self performSelector:@selector(download:) withObject:url afterDelay:3];
// 利用sleep实现延迟(不要用这个,会卡住主线程,即后面的动作不会执行)
[NSThread sleepForTimeInterval:3];

本文介绍了在iOS开发中使用GCD(Grand Central Dispatch)进行延迟执行的方法,包括如何在主线程及子线程中实现延迟任务,同时也提到了使用sleep方法带来的问题。
&spm=1001.2101.3001.5002&articleId=46798531&d=1&t=3&u=bebe9e6ee6384447b7532baf06725b2e)
1796

被折叠的 条评论
为什么被折叠?



