gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:45:35 26 4
gpt4 key购买 nike

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 上。谢谢!

最佳答案

我刚刚收到 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;

关于swift - OperationQueue 的“进度”属性在 iOS 13 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60741067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com