由于苹果网站上缺少文档,我问这个问题。是否有任何方法可以链接到CKOperationGroup中的CKOperation以依次执行?特别是,我对两个CKModifyRecordsOperation感兴趣,我需要一个接一个地执行。
非常感谢
而不是使用CKOperation.completionBlock
,而不是使用OperationQueue
?这是我想出的。它是完全未经测试的,所以让我知道它是否无用。
class CKOperationQueue: NSObject {
private var completionHandler: (() -> Void)?
private var isExecuting = false
private let operations: [CKOperation]
private let queue: OperationQueue
init(operations: [CKOperation]) {
self.operations = operations
queue = OperationQueue()
queue.name = "CKOperationQueue"
queue.maxConcurrentOperationCount = 1
}
@discardableResult func execute() -> CKOperationQueue {
guard !isExecuting else { return self }
isExecuting = true
queue.addOperations(operations, waitUntilFinished: false)
queue.addOperation {
self.completionHandler?()
}
return self
}
@discardableResult func onCompletion(_ handler: @escaping () -> Void) -> CKOperationQueue {
self.completionHandler = handler
return self
}
}