美文网首页
Tools with swift

Tools with swift

作者: easy_luo | 来源:发表于2021-02-24 00:21 被阅读0次
//日志打印
func log<T>(_ msg: T,
            file: NSString = #file,
            line: Int = #line,
            fn: String = #function) {
    #if DEBUG
    let prefix = "\(file.lastPathComponent)_\(line)_\(fn)"
//    prefix + (msg as! String)
    print(prefix,msg)
    #endif
}

//GCD异步函数封装
public typealias Task = () -> Void

public struct Asynchrony {
    public static func async(_ task: @escaping Task) {
        _async(task)
    }

    public static func async(_ task: @escaping Task,
                      _ mainTask: @escaping Task){
        _async(task, mainTask)
    }

    private static func _async(_ task: @escaping Task,
                       _ mainTask: Task? = nil){
        let item = DispatchWorkItem(block: task)
        DispatchQueue.global().async(execute: item)
        if let main = mainTask {
            item.notify(queue: DispatchQueue.main, execute: main)
        }
    }
    
    @discardableResult
    public static func delayOnAsyncThread(_ seconds: Double,
                                          _ task: @escaping Task) -> DispatchWorkItem {
        _delay(seconds, task)
    }

    @discardableResult
    public static func delayOnAsyncThread(_ seconds: Double,
                                          _ task: @escaping Task,
                                          _ mainTask: @escaping Task)  -> DispatchWorkItem {
        _delay(seconds, task, mainTask)
    }
    
    @discardableResult
    private static func _delay(_ seconds: Double,
                             _ task: @escaping Task,
                             _ mainTask: Task? = nil) -> DispatchWorkItem{
        let item = DispatchWorkItem(block: task)
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + seconds, execute: item)
        if let main = mainTask {
            item.notify(queue: DispatchQueue.main, execute: main)
        }
        return item
    }
    
    @discardableResult
    public static func delayOnMainThread(_ seconds: Double,
                             _ task: @escaping Task) -> DispatchWorkItem{
        let item = DispatchWorkItem(block: task)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds, execute: item)
        return item
    }
}

func testAsync() {
    Asynchrony.async{ print(Thread.current) }

    Asynchrony.async ({
        print(Thread.current)
    }){
        print(Thread.current)
    }
    
    Asynchrony.delayOnAsyncThread(1, {
        print("async delay 1",Thread.current)
    })
        
    Asynchrony.delayOnAsyncThread(2) {
        print("async delay 2",Thread.current)
    } _: {
        print("async delay 2, call back on main",Thread.current)
    }
    
    Asynchrony.delayOnMainThread(3) {
        print("async on main Thread delay 3",Thread.current)
    }
}

相关文章

  • Tools with swift

  • swift转oc (互转)

    //swift转oc (互转) http://www.ucai.cn/index.php?app=tools&mo...

  • Swift严格的单例写法

    相比OC,Swift有很优雅的实现单例的写法。 实现 单例类Tools 客户端调用: 说明 当尝试使用 这种方法去...

  • 论文日鉴16:综合生信工具网站

    Bio.tools bio.tools · Bioinformatics Tools and Services D...

  • 简谈platform-tools,build-tools,too

    如果所示,platform-tools,build-tools,tools几个文件夹。 plat...

  • centos 7 setup minikube k8s env

    tools These tools just for virtual cloud compute machine ...

  • Tools

    完全理解Gson(3):Gson反序列化 完全理解Gson(2):Gson序列化 完全理解Gson(1):简单入门...

  • Tools

    GitHub; Google Browser:already comes with CDT (Chrome Dev...

  • Tools

    cut cut这个工具主要是用来从文本中抽取文本的今天我主要学习的使用是这样的情况,举个例子吧。我们创建一个jyq...

  • Tools

    1.CocoaPods:http://ishalou.com/blog/2012/10/15/how-to-use...

网友评论

      本文标题:Tools with swift

      本文链接:https://www.haomeiwen.com/subject/iiljfltx.html