你可能想看看NSOperationQueue
,文档页面在这里。
还有两个现有的 Stack Overflow 问题可以解决使用的问题,这里NSOperationQueue
是这里和这里。两者都接受了答案。
至于确定文件写入是否已完成,我在编写创建一个非常大的文件的 OS X 应用程序时也遇到了这个问题。我希望用户能够跟踪写入的进度。我最终使用了一个NSTimer
和一个UIProgressBar
本质上,您需要确定文件的(预期)总大小。然后,当您编写文件时,您应该有另一种方法(在下面的代码中checkFileWritingProgress
),您可以使用NSTimer
. 根据预期文件总大小检查当前进度并UIProgressBar
相应更新。
我提供了一些代码来帮助您入门。
- (void)checkFileWritingProgress:(NSTimer *)someTimer {
fileAttributes = [fileManager attributesOfItemAtPath:[NSString stringWithFormat:@"%@.data",saveLocation] error:nil];
currentFileSize = [fileAttributes fileSize]; // instance variable
if(currentFileSize < maxFileSize) { // maxFileSize is instance variable
[progressBar setDoubleValue:(((double)currentFileSize/(double)maxFileSize)*100)];
// progressWindows OS X only... not on iOS
//[progressWindow setTitle:[NSString stringWithFormat:@"Writing... | %.0f%%",(((double)currentFileSize/(double)maxFileSize)*100)]];
}
else {
[progressBar setDoubleValue:100.0];
}
}
还有计时器...
NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(checkFileWritingProgress:)
userInfo:nil
repeats:YES] retain];
//(linked to timer... this is the
// CORRECT way to use a determinate progress bar)
[progressBar setIndeterminate:NO];
[progressBar setDoubleValue:0.0];
[progressBar displayIfNeeded];
我希望这段代码有所帮助。让我知道是否需要澄清任何事情。