为什么 Progress.fractionCompleted 超过 1.0?

Why is Progress.fractionCompleted exceeding 1.0?

我注意到在某些情况下 Progress.fractionCompleted 超过 1.0 甚至重置回 0。我首先注意到它是在使用 MultipeerConnectivity 框架时,我在其中构建了一个进度树,其中包含从 [=14= 返回的 Progress 实例].

下面的代码重现了奇怪的行为。只需复制粘贴即可。

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时,它可以正常工作。

我认为这是一个错误,但在报告之前,我想确保我没有遗漏任何东西。

为了消除怀疑来源,我进行了各种练习,例如在 child 进度的每次更改之前添加延迟,摆弄各种数字,甚至将整个事情翻译成 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 的 parent 分数结束。

  • 如果 sz 等于或小于 5,则完成的 parent 分数最终为 1。问题似乎只有在 child 计数超过某个数字时才会出现。

  • 通过添加延迟来更改时间没有区别(此处未显示代码)。

  • iOS 12 上的完全相同的代码 运行 总是得到 1 的 parent 分数!

因此,虽然要说服我苹果可能犯了这样的错误需要花费很多时间,但我 说服了。您在 iOS 13 Foundation 中发现了一个非常讨厌的错误。