Swift 让 Async 帮你解决线程问题

使用 Async 帮你解决常见的线程问题 ?

线程这种东西一般封装比较深,很微观,也很考察计算机基础知识。


? 要求

  • iOS 9.0+
  • Xcode 9.0+
  • Swift 4

? 测试 UI 什么样子?

1.展示页2.展示页3.展示页
常见场景列表耗时操作场景示例黑科技操作场景示例

? 安装方法

安装

iOS, 你需要在 Podfile 中添加.

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'AsyncSwift'
复制代码

? 配置

创建表单

1.耗时操作

这是应用最广泛的场景,为了避免阻塞主线程,将耗时操作放在子线程处理,然后在主线程使用处理结果。比如读取沙盒中的一些数据,然后将读取的数据展示在 UI,这个场景还有几个细分: 1.1 执行一个耗时操作后回调主线程

Async.background {
print("A: This is run on the \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))")
    sleep(2)
}.main {
    print("B: This is run on the \(qos_class_self().description) (expected \(qos_class_main().description)), after the previous block")
}
复制代码

1.2 串行耗时操作

每一段子任务依赖上一个任务完成,全部完成后回调主线程:

let backgroundBlock = Async.background {
    print("This is run on the first\(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))")
    sleep(2)
    
    print("This is run on the second \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description))")
    sleep(2)
}
// Run other code here...
backgroundBlock.main {
    print("This is run on the \(qos_class_self().description) (expected \(qos_class_main().description)), after the previous block")
}
复制代码

1.3 并发耗时操作

每一段子任务独立,所有子任务完成后回调主线程:

Async.main {
    print("This is run on the \(qos_class_self().description) (expected \(qos_class_main().description))")
    // Prints: "This is run on the Main (expected Main) count: 1 (expected 1)"
    }.userInteractive {
        print("This is run on the \(qos_class_self().description) (expected \(QOS_CLASS_USER_INTERACTIVE.description))")
        // Prints: "This is run on the Main (expected Main) count: 2 (expected 2)"
    }.userInitiated {
        print("This is run on the \(qos_class_self().description) (expected \(QOS_CLASS_USER_INITIATED.description)) ")
        // Prints: "This is run on the User Initiated (expected User Initiated) count: 3 (expected 3)"
    }.utility {
        print("This is run on the \(qos_class_self().description) (expected \(QOS_CLASS_UTILITY.description)) ")
        // Prints: "This is run on the Utility (expected Utility) count: 4 (expected 4)"
    }.background {
        print("This is run on the \(qos_class_self().description) (expected \(QOS_CLASS_BACKGROUND.description)) ")
        // Prints: "This is run on the User Interactive (expected User Interactive) count: 5 (expected 5)"
}
复制代码

2.延时执行

延时一段时间后执行代码,一般见于打开 App 一段时间后,弹出求好评对话框。

let seconds = 3.0
Async.main(after: seconds) {
    print("Is called after 3 seconds")
}.background(after: 6.0) {
    print("At least 3.0 seconds after previous block, and 6.0 after Async code is called")
}
复制代码

其他用法请见 Demo。


⚖ 协议

MIT License

Copyright (c) 2017 ReverseScale

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
复制代码

? 联系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值