我注意到在某些情况下Progress.fractionCompleted
超过1.0,甚至重置回0。我第一次注意到它是在使用多对等连接框架时,我在那里构建了一个进度树,其中包含从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 时,它可以正常工作。
我认为这是一个错误,但在报告之前,我想确保我没有遗漏某些东西。
为了消除怀疑的来源,我经历了各种练习,例如在孩子进度的每个变化之前添加延迟,摆动各种数字,甚至将整个事情转换为 Objective-C 以确保这不是某种 Swift 数字问题:
int64_t totals[] = {34375, 19110, 37511, 18799, 42705, 19021};
int64_t initials[] = {9855, 10950, 9855, 9855, 9855, 10950};
int sz = 6;
NSProgress* parent = [NSProgress progressWithTotalUnitCount:sz];
NSMutableArray* children = [NSMutableArray array];
for (int i = 0; i<sz; i++) {
NSProgress* child = [NSProgress progressWithTotalUnitCount:totals[i]];
child.completedUnitCount = initials[i];
[children addObject:child];
[parent addChild:child withPendingUnitCount:1];
}
for (int i = 0; i<sz; i++) {
NSProgress* child = children[i];
child.completedUnitCount = totals[i];
}
NSLog(@"%@", parent);
我的结论是:
即使在 Objective-C 中,上面的代码最终也会以
1.2795
完成的父分数结束。如果
sz
为 5 或更小,则完成的父分数最终为1
。似乎只有当子计数超过某个数字时才会出现此问题。通过添加延迟来更改计时没有任何区别(此处未显示代码(。
在 iOS 12 上运行的相同代码总是完成
1
的父部分!
因此,虽然说服我苹果可能犯了这样的错误需要很多时间,但我被说服了。您在iOS 13 Foundation中发现了一个非常讨厌的错误。