OperationQueue 的“进度”属性在 iOS 13 中不起作用

Geo*_*org 2 nsoperationqueue progress-bar swift ios13

iOS 13progressOperationQueue类中引入了该属性。同时,Apple 将operationsoperationCount属性标记为已弃用,这表明它们不应再用于报告队列进度。

我的问题是我无法让该progress属性按照我期望的方式工作(这基本上是开箱即用的)。此外,我找不到有关此新属性的任何文档(除了它现在存在的文档)。

我试图让它在一个新的 SingleView 项目中工作,该项目UIProgressView在 main 上有一个UIViewController。该示例深受https://nshipster.com/ios-13/的启发。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var progressView: UIProgressView!

    private let operationQueue: OperationQueue = {

        let queue = OperationQueue()
        queue.maxConcurrentOperationCount = 1
        queue.underlyingQueue = .global(qos: .background)

        return queue

    }()

    override func viewDidLoad() {

        super.viewDidLoad()

        self.progressView.observedProgress = operationQueue.progress

        self.operationQueue.cancelAllOperations()
        self.operationQueue.isSuspended = true

        for i in 0...9 {

            let operation = BlockOperation {
                sleep(1)
                NSLog("Operation \(i) executed.")
            }

            self.operationQueue.addOperation(operation)

        }

    }

    override func viewDidAppear(_ animated: Bool) {

        self.operationQueue.isSuspended = false

    }

}
Run Code Online (Sandbox Code Playgroud)

控制台显示队列正在按预期运行(作为串行队列),但进度条上没有任何移动。

此外,直接对属性进行 KVOprogress不起作用,因此我怀疑progressOperationQueue 的属性是问题的原因,而不是UIProgressView.

知道我在这里缺少什么吗?或者这可能是 iOS 13 中的一个错误?该问题存在于模拟器和 iPhone 6s Plus 上,两者都运行 iOS 13.3.1。谢谢!

Geo*_*org 7

我刚刚收到苹果公司关于此事的反馈。该文档目前很好地隐藏在头文件 NSOperation.h 中。以下是遇到同样问题的人的摘录:

/// @property progress
/// @discussion     The `progress` property represents a total progress of the operations executed in the queue. By default NSOperationQueue
/// does not report progress until the `totalUnitCount` of the progress is set. When the `totalUnitCount` property of the progress is set the
/// queue then opts into participating in progress reporting. When enabled, each operation will contribute 1 unit of completion to the
/// overall progress of the queue for operations that are finished by the end of main (operations that override start and do not invoke super
/// will not contribute to progress). Special attention to race conditions should be made when updating the `totalUnitCount` of the progress
/// as well as care should be taken to avoid 'backwards progress'. For example; when a NSOperationQueue's progress is 5/10, representing 50%
/// completed, and there are 90 more operations about to be added and the `totalUnitCount` that would then make the progress report as 5/100
/// which represents 5%. In this example it would mean that any progress bar would jump from displaying 50% back to 5%, which might not be
/// desirable. In the cases where the `totalUnitCount` needs to be adjusted it is suggested to do this for thread-safety in a barrier by
/// using the `addBarrierBlock:` API. This ensures that no un-expected execution state occurs adjusting into a potentially backwards moving
/// progress scenario.
///
/// @example
/// NSOperationQueue *queue = [[NSOperationQueue alloc] init];
/// queue.progress.totalUnitCount = 10;
Run Code Online (Sandbox Code Playgroud)