swift 多线程的使用

尝试了下Swift3.0中GCD的延时函数与之前的有挺大的不同 - swift3.0中有4个延迟函数,如下

public func asyncAfter(deadline: DispatchTime, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: @escaping @convention(block) () -> Swift.Void)

public func asyncAfter(wallDeadline: DispatchWallTime, qos: DispatchQoS = default, flags: DispatchWorkItemFlags = default, execute work: @escaping @convention(block) () -> Swift.Void)

public func asyncAfter(deadline: DispatchTime, execute: DispatchWorkItem)

public func asyncAfter(wallDeadline: DispatchWallTime, execute: DispatchWorkItem)

一般开发中,用第三个应该就足够了,下面看看具体使用

public func asyncAfter(deadline: DispatchTime, execute: DispatchWorkItem)  
// 该方法接收一个DispatchTime的参数,点进这个类型的里面可以发现实个结构体
public struct DispatchTime : Comparable {  
    public let rawValue: dispatch_time_t
    public static func now() -> DispatchTime
    public static let distantFuture: DispatchTime
    public init(uptimeNanoseconds: UInt64)
    public var uptimeNanoseconds: UInt64 { get }
}
// 
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: {  
            self.tableView?.reloadData()
            if self.refreshControl?.isRefreshing == true {
                self.refreshControl?.endRefreshing()
            }
        })

DispatchTime.now() 获取当前时间,  
DispatchTime.now() + 2, 在当前时间的基础上加2,那就是延时两秒了
posted @ 2016-12-23 09:53  尸者禾火雨相  阅读(940)  评论(0编辑  收藏  举报