【发布时间】:2018-06-28 14:59:41
【问题描述】:
由于文件加载可能很长(大型文本文件需要 15-20 秒),我想显示一个等待窗口,其中包含未确定的 NSProgressIndicator,以帮助用户耐心等待。我为此创建了一个笔尖和一个窗口控制器。问题是在应用程序启动时最近加载文件期间在屏幕上显示此窗口。
在 NSDocumentSubclass 的makeWindowControllers 中,我放了以下代码:
...
dispatch_async(dispatch_get_main_queue(), ^(){
self->progressController = [[ProgressViewWindowController alloc] initWithWindowNibName:@"ProgressViewWindowController"];
[self->progressController.progressIndicator setUsesThreadedAnimation:YES];
[self->progressController.progressIndicator startAnimation:self];
[self->progressController showWindow:self];
});
...
在initForURL:(NSURL *)urlOrNil withContentsOfURL:...我把下面的代码放在最后:
self.content = [[NSMutableString alloc] initWithContentsOfURL:self.fileURL encoding:encoder error:&error];
dispatch_async(dispatch_get_main_queue(), ^(){
[self->progressController close];
});
但是只有在 NSDocumentSubclass 完成加载并且其内容显示在屏幕上之后才会显示窗口,而不是之前。因此,永远不会进行关闭调用(我想它是在显示窗口之前调用的)。如果我删除 dispatch_async... 块,什么都不会显示,既不及时也不延迟。
当我询问这个窗口时,如何才能显示它?我应该用异步方法替换同步[[NSMutableString alloc] initWithContentsOfURL:self.fileURL encoding:encoder error:&error]; 方法以释放主线程上的时间吗?如何异步读取文件中的字符串? (我在将YES 返回到+ (BOOL) canConcurrentlyReadDocumentsOfType:(NSString *)typeName 时测试了并发文件读取,但它没有改变任何东西)
【问题讨论】:
标签: grand-central-dispatch nsdocument file-read nsprogressindicator