【问题标题】:Displaying a window during NSDocumentSubclass initialization在 NSDocumentSubclass 初始化期间显示一个窗口
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多