NSWindow 不会在应用程序启动后立即绘制

NSWindow won't draw immediately after app launch

问题:如果立即调用其他处理器密集型非UI 代码,尝试显示带有来自 applicationWillFinishLaunching 的文本的 window 将不会自行绘制。

背景:我有一个助手应用程序,启动时可能会或可能不会与最终用户交互。虽然它 "deciding" 如果它需要提出一个 window 来询问用户问题,可能会有 1 秒到 10 秒的时间过去(启动后它在非 UI能够通过 Internet 通信的库代码)。

所以我想善待用户并在进入该库代码之前将 "mini-alert"* window 和 "working, please wait..." 放在一起处理已过。

似乎应用程序本身在启动后甚至没有时间绘制这个迷你警报(它只是一个 NSWindow,带有 NSView,一些文本,没有按钮)。

如果在库代码 returns 之后想要为用户发出错误警报或查询 window -- 那么此时迷你警报会按预期绘制。但是,如果我关闭迷你警报(见下文)然后设置 NSAlert - 迷你警报没有足够的时间自行关闭。

- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
    [NSApp activateIgnoringOtherApps:YES];

    briefAlertWindowController = [[NSWindowController alloc] initWithWindowNibName:@"BriefAlertWindow"];

    [[briefAlertWindowController window] center];
    [briefAlertWindowController showWindow:self ];
    [[briefAlertWindowController window] orderFront:self ];
    [[briefAlertWindowController window] display];
    [[briefAlertWindowController window] makeKeyAndOrderFront:nil];
}

并关闭迷你警报:

- (void)dismissMiniAlert
{
    NSWindow * theWindow = [briefAlertWindowController window];
    [theWindow orderOut:nil];
}

请注意,NSWindow 和 NSWindowController 都没有 derived/subclassed 此迷你警报。

听起来像是线程问题。 splash window 无法在主线程上绘制自己,因为主线程正忙于执行处理器密集型操作。正确地,您的处理器密集型工作应该全部发生在后台线程上。如果你做不到,你至少需要离开主线程足够长的时间,让 runloop 有机会绘制你的 window。只是引入延迟。