我尝试理解创建树NSProgresses的逻辑。但对我来说并非一切顺利。我尝试实现全局NSProgress管理器,它将观察应用程序中的所有任务和操作,并要求在任务之一未完成时请求静默。在我的想象中,它应该是根NSProgress,子进程将附加。当我们完全应用时,我们需要询问fractionCompleted。这意味着根NSProgress将递归遍历所有子NSProgress并计算总进度。但在实践中却没有。 我制作了用作测试台的代码(http://pastebin.com/CbXiFwCi)。请帮助我理解NSProgress逻辑,因为网络纯文档。谢谢。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.progress = [[NSProgress alloc] initWithParent:nil
userInfo:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self.progress becomeCurrentWithPendingUnitCount:0]; ///what I should set as pendingUnitCount?
NSProgress * progressA = [NSProgress progressWithTotalUnitCount:INT64_MAX-1];
progressA.cancellable = NO;/// set cancellable to NO
for(NSUInteger i = 0; i < INT64_MAX; i++)
{
//do work
progressA.completedUnitCount ++;
}
[self.progress resignCurrent];
});
dispatch_async(dispatch_get_main_queue(), ^{
[self.progress becomeCurrentWithPendingUnitCount:0];///what I should set as pendingUnitCount?
NSProgress * progressB = [NSProgress progressWithTotalUnitCount:150];
for(NSUInteger i = 0; i < 150; i++)
{
//do work
progressB.completedUnitCount ++;
}
[self.progress resignCurrent];
});
}
- (void)action:(id)sender
{
double fractionCompleted = self.progress.fractionCompleted;///why fractionCompleted -> NAN?
BOOL cancellable = self.progress.cancellable; ///why cancellable is YES if progressA.cancellable -> NO
}