在NSDocumentSubclass初始化期间显示窗口

时间:2018-06-28 14:59:42

标签: grand-central-dispatch nsdocument file-read nsprogressindicator

由于文件加载时间可能很长(大型文本文件需要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加载完成并且其内容显示在屏幕上之后才显示窗口,而不是之前。因此,永远不会进行close调用(我想是在显示窗口之前调用它)。如果我删除dispatch_async ...块,则什么都不会显示,无论是及时显示还是延迟显示。

问到如何显示此窗口?我应该用一个异步方法代替同步[[NSMutableString alloc] initWithContentsOfURL:self.fileURL encoding:encoder error:&error];方法来释放主线程上的时间吗?如何异步读取文件中的字符串? (我测试了将YES返回到+ (BOOL) canConcurrentlyReadDocumentsOfType:(NSString *)typeName时读取的并发文件,但它没有任何改变)

1 个答案:

答案 0 :(得分:0)

如果在Application委托中使用了相同的代码而不是NSDocumentControllerSubclass,则窗口将正确显示。

相关问题