'progress' 属性 的 OperationQueue 在 iOS 13 中不工作
'progress' property of OperationQueue not working in iOS 13
iOS13在OperationQueue
class中引入了progress
属性。同时,Apple 将 operations
和 operationCount
属性标记为已弃用,这表明不应再使用它们来报告队列的进度。
我的问题是我无法让 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 中的错误?该问题存在于模拟器以及 iPhone 6s Plus 和 运行 iOS 13.3.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;
iOS13在OperationQueue
class中引入了progress
属性。同时,Apple 将 operations
和 operationCount
属性标记为已弃用,这表明不应再使用它们来报告队列的进度。
我的问题是我无法让 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 中的错误?该问题存在于模拟器以及 iPhone 6s Plus 和 运行 iOS 13.3.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;