summaryrefslogtreecommitdiffhomepage
path: root/ios/Operations/GroupOperation.swift
blob: 339330c6aca74ebf6468a328390cf7ce7df3a3a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
//  GroupOperation.swift
//  Operations
//
//  Created by pronebird on 31/05/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation

public final class GroupOperation: AsyncOperation, @unchecked Sendable {
    private let operationQueue = AsyncOperationQueue()
    private let children: [Operation]

    public init(operations: [Operation]) {
        children = operations

        super.init(dispatchQueue: nil)
    }

    override public func main() {
        let finishingOperation = BlockOperation()
        finishingOperation.completionBlock = { [weak self] in
            self?.finish()
        }
        finishingOperation.addDependencies(children)

        operationQueue.addOperations(children, waitUntilFinished: false)
        operationQueue.addOperation(finishingOperation)
    }

    override public func operationDidCancel() {
        operationQueue.cancelAllOperations()
    }
}