iOS 13在progress
类中引入了OperationQueue
属性。同时,Apple将operations
和operationCount
属性标记为已弃用,这表明它们不应再用于报告队列中的进度。
我的问题是,我无法使progress
属性正常工作(基本上是开箱即用的)。另外,我找不到有关此新属性的任何文档(现在不存在)。
我试图让它在新的SingleView项目中工作,该项目在主UIProgressView
上有一个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
}
}
控制台显示队列正在按预期方式运行(作为串行队列,但是进度条上没有移动。
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;