我注意到在某些情况下Progress.fractionCompleted
超过 1.0 甚至重置回 0。我第一次注意到它是在使用 MultipeerConnectivity 框架时,我在其中构建了一个进度树,其中 Progress 实例从MCSession.sendResource
.
下面的代码重现了奇怪的行为。只需复制粘贴即可。
let childUnitCounts: [(initialCompletedUnitCount: Int64, totalUnitCount: Int64)] = [
(9855, 34375),
(10950, 19110),
(9855, 37511),
(9855, 18799),
(9855, 42705),
(10950, 19021), // When commenting out this line or setting the initial unit count to 0, things work as expected.
]
let parent = Progress(totalUnitCount: Int64(childUnitCounts.count))
let children: [Progress] = childUnitCounts.map { unitCount in
let child = Progress(totalUnitCount: unitCount.totalUnitCount)
child.completedUnitCount = unitCount.initialCompletedUnitCount
parent.addChild(child, withPendingUnitCount: 1)
return child
}
print(parent)
for (i, state) in childUnitCounts.enumerated() {
children[i].completedUnitCount = state.totalUnitCount
print(parent)
}
以上产生以下输出:
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 0.4088 / Completed: 0 of 6
<NSProgress: 0x600000f6dc20> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2627 / Completed: 9855 of 37511
<NSProgress: 0x600000f6db80> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5730 / Completed: 10950 of 19110
<NSProgress: 0x600000f6dae0> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2867 / Completed: 9855 of 34375
<NSProgress: 0x600000f6de00> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5757 / Completed: 10950 of 19021
<NSProgress: 0x600000f6dd60> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2308 / Completed: 9855 of 42705
<NSProgress: 0x600000f6dcc0> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5242 / Completed: 9855 of 18799
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 0.5277 / Completed: 1 of 6
<NSProgress: 0x600000f6dc20> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2627 / Completed: 9855 of 37511
<NSProgress: 0x600000f6db80> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5730 / Completed: 10950 of 19110
<NSProgress: 0x600000f6de00> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5757 / Completed: 10950 of 19021
<NSProgress: 0x600000f6dd60> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2308 / Completed: 9855 of 42705
<NSProgress: 0x600000f6dcc0> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5242 / Completed: 9855 of 18799
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 0.5989 / Completed: 2 of 6
<NSProgress: 0x600000f6dc20> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2627 / Completed: 9855 of 37511
<NSProgress: 0x600000f6de00> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5757 / Completed: 10950 of 19021
<NSProgress: 0x600000f6dd60> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2308 / Completed: 9855 of 42705
<NSProgress: 0x600000f6dcc0> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5242 / Completed: 9855 of 18799
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 0.8094 / Completed: 3 of 6
<NSProgress: 0x600000f6de00> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5757 / Completed: 10950 of 19021
<NSProgress: 0x600000f6dd60> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2308 / Completed: 9855 of 42705
<NSProgress: 0x600000f6dcc0> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5242 / Completed: 9855 of 18799
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 0.8886 / Completed: 4 of 6
<NSProgress: 0x600000f6de00> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5757 / Completed: 10950 of 19021
<NSProgress: 0x600000f6dd60> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.2308 / Completed: 9855 of 42705
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 1.0169 / Completed: 5 of 6
<NSProgress: 0x600000f6de00> : Parent: 0x600000f6da40 (portion: 1) / Fraction completed: 0.5757 / Completed: 10950 of 19021
<NSProgress: 0x600000f6da40> : Parent: 0x0 (portion: 0) / Fraction completed: 1.2795 / Completed: 6 of 6
最后一行显然应该以Fraction completed: 1.0
. 注释掉最后一个元素childUnitCounts
或将初始单元计数设置为0时,它可以正常工作。
我认为这是一个错误,但在报告它之前,我想确保我没有遗漏任何东西。