2

我注意到在某些情况下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时,它可以正常工作。

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

4

1 回答 1

1

为了消除怀疑的来源,我进行了各种练习,例如在孩子进步的每次变化之前添加延迟,玩弄各种数字,甚至将整个事情翻译成 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 中发现了一个非常严重的错误。

于 2019-11-22T00:41:39.513 回答