1.dispatch_block_cancel
iOS8之后可以调用dispatch_block_cancel
来取消(需要注意必须用dispatch_block_create
创建dispatch_block_t
)
let task2 = dispatch_block_create(DISPATCH_BLOCK_BARRIER, { print("output after 5 seconds") })
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), task2)
dispatch_block_cancel(task2)
2.Swift模拟取消
实际上delay时间到了之后还是会执行延时block,只是这时真正要执行的任务block已经为空
import Foundation
typealias Task = (cancel: Bool) -> Void
func gcdDelay(time: NSTimeInterval, task: ()->()) -> Task? {
func dispatch_later(block: ()->()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), block)
}
var closure: dispatch_block_t? = task
var result: Task?
let delayedClosure: Task = {
cancel in
if let internalClosure = closure {
if cancel == false {
dispatch_async(dispatch_get_main_queue(), internalClosure)
}
}
closure = nil
result = nil
}
result = delayedClosure
dispatch_later {
if let delayedClosure = result {
delayedClosure(cancel: false)
}
}
return result
}
func gcdCancel(task: Task?) {
task?(cancel: true)
}
func gcdTest(){
let task = gcdDelay(5) {
print("output after 5 seconds")
}
gcdCancel(task)
}
3.Objective-C模拟取消
原理与2类似
GCDDelay.h
#import <Foundation/Foundation.h>
typedef void(^GCDTask)(BOOL cancel);
typedef void(^gcdBlock)();
@interface GCDDelay : NSObject
+(GCDTask)gcdDelay:(NSTimeInterval)time task:(gcdBlock)block;
+(void)gcdCancel:(GCDTask)task;
+(void)gcdTest;
@end
GCDDelay.m
@implementation GCDDelay
+(void)gcdLater:(NSTimeInterval)time block:(gcdBlock)block
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}
+(GCDTask)gcdDelay:(NSTimeInterval)time task:(gcdBlock)block
{
__block dispatch_block_t closure = block;
__block GCDTask result;
GCDTask delayedClosure = ^(BOOL cancel){
if (closure) {
if (!cancel) {
dispatch_async(dispatch_get_main_queue(), closure);
}
}
closure = nil;
result = nil;
};
result = delayedClosure;
[self gcdLater:time block:^{
if (result)
result(NO);
}];
return result;
}
+(void)gcdCancel:(GCDTask)task
{
task(YES);
}
+(void)gcdTest
{
GCDTask task = [self gcdDelay:5 task:^{
NSLog(@"oc output after 5 seconds");
}];
[self gcdCancel:task];
}
@end
参考:
1.swifter.tips之GCD和延时调用