【问题标题】:'progress' property of OperationQueue not working in iOS 13OperationQueue 的“进度”属性在 iOS 13 中不起作用
【发布时间】:2020-03-18 13:55:55
【问题描述】:

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

我的问题是我无法让progress 属性像我期望的那样工作(这基本上是开箱即用的)。我也找不到关于这个新属性的任何文档(除了它现在存在)。

我试图让它在一个新的 SingleView 项目中工作,该项目在主 UIViewController 上有一个 UIProgressView。此示例深受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

    }

}

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

progress 属性上的 KVO 也直接不起作用,所以我怀疑 OperationQueue 的 progress 属性是问题的原因,而不是 UIProgressView

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

【问题讨论】:

    标签: swift progress-bar ios13 nsoperationqueue


    【解决方案1】:

    我刚刚收到 Apple 对此的反馈。该文档目前很好地隐藏在头文件 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;
    

    【讨论】:

    • 在 Apple 发布的文档中,他们声明异步操作应该覆盖 start()。这是不正确的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多