DQExtensions 0.2.0

DQExtensions 0.2.0

Sahel Jalal维护。



DQExtensions

Version Platform License

DQExtensions

功能

该库为在Grand Central Dispatch中管理DispatchQueue类型添加了几个有用的扩展和便捷功能。

Apple提供了强大的功能,但这个库解决了以下问题

  • 创建命名 dispatch queues 的统一机制
  • 识别当前正在运行的队列
  • 检查当前是否在主队列上或任何特定队列上
  • 消除同步调用中的风险,如果检测到相同的队列,则会内联执行
  • 简单化的常见操作 API

安装

Swift Package Manager

项目支持 SPM,并且可以添加到针对 Swift 5.3 及更高版本的项目的依赖中。

在 Xcode 中,转到菜单项目 -> Swift 包,选择添加包依赖。将此 GitHub 位置粘贴到文本字段,并将目标添加到项目中。

Cocoapods

更新你的 Podfile 并运行 pod install

pod 'Boing'

Carthage

更新你的 Cartfile 并运行 carthage update

github "pokanop/dqe"

API

全局变量与方法

DQExtensions 需要调用一个方法来启动其功能。在生命周期早期,应该调用 initialize

DQExtensions.initialize()

要检查是否在 主线程 上运行,请使用 isMainQueue 全局属性

guard isMainQueue else { return }

调用者可以使用 queueName 全局属性来获取队列的名称

print("running on queue: \(queueName)")

如果可能,要访问当前正在运行的队列,请使用 currentQueue 全局属性

guard let currentQueue = currentQueue else { return }

currentQueue.async {
    // Do work
}

请注意,此属性仅在 GCD 供应的队列上有效,而不是自定义队列,因为那些不是作为相关队列跟踪到名称中。

DispatchQueue 扩展

为了充分利用 DQExtensions 的能力,用户应该使用便利初始化器或对已创建的私有队列使用 set(name:) 方法。这允许扩展能够确定性地识别队列并做出同步发送和安全比较队列等决策。

便利初始化器

///
/// Create a custom `DispatchQueue` with a name and associated attributes.
///
/// - Parameters:
///   - name: The name of the queue to set.
///   - label: A unique label to set on the queue for debugging purposes.
///   - qos: The quality-of-service level to associate with the queue.
///   - attributes: The attributes to associate with the queue
///   - autoreleaseFrequency: The frequency with which to autorelease objects created by the blocks that the queue schedules
///   - target: The target queue on which to execute block.
///
convenience init(name: String,
                 label: String = "",
                 qos: DispatchQoS = .unspecified,
                 attributes: DispatchQueue.Attributes = [],
                 autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = .inherit,
                 target: DispatchQueue? = nil)

设置队列名称

///
/// Set the name of the queue.
///
/// - Parameter name: The name of the queue to set.
///
func set(name: String)

DQExtensions 提供了几个类似于其他 QoS 并行队列的 DispatchQueue.global() 方法的静态访问器。

static func background() -> DispatchQueue
static func utility() -> DispatchQueue
static func `default`() -> DispatchQueue
static func userInitiated() -> DispatchQueue
static func userInteractive() -> DispatchQueue

假设扩展已初始化,且队列已使用名称创建,或之后设置了名称,DQExtensions 提供了属性来检查队列是否是当前执行队列以及名称。

///
/// The associated name of the queue.
///
var name: String?

///
/// Returns true if this queue is the currently executing queue.
///
var isCurrent: Bool

在队列上同步召集通常会是灾难性的,如果是从同一个队列中召集的话。以阻塞方式执行的目的会在已经在同一个队列上的情况下产生死锁。DQExtensions提供了一个syncSafe(work:)方法,它将通过在行内运行闭包以期望的方式短路这种危险行为。

///
/// Submits a closure for synchronous execution on this queue safely.
///
/// If the queue is already executing, the `work` is executed inline without dispatching
/// synchronously which would otherwise lead to a deadlock.
///
/// - Parameter work: The closure to execute.
///
func syncSafe(_ work: () -> ())

贡献

贡献使得开源社区成为一个如此非凡的学习、灵感和创造的地方。您做出的任何贡献都将被强烈赞赏。

  1. 仓库分叉
  2. 创建您的功能分支(git checkout -b feature/AmazingFeature
  3. 提交您的更改(git commit -m 'Add some AmazingFeature'
  4. 推送分支(git push origin feature/AmazingFeature
  5. 打开 pull 请求

许可

分发在MIT许可下。