简体   繁体   English

防止基于 NSDocument 的应用程序在崩溃后重新打开文档

[英]Prevent NSDocument-based app from reopening documents after a crash

I have a read-only music player app for macOS that uses NSDocument to get all the file-handling logic for free.我有一个适用于 macOS 的只读音乐播放器应用程序,它使用 NSDocument 免费获取所有文件处理逻辑。

The problem that I now have is that every time the app crashes (or is stopped by the debugger) while one or more player windows are open, they automatically get reopened when the app is restarted.我现在遇到的问题是,每次当一个或多个播放器窗口打开时应用程序崩溃(或被调试器停止)时,它们会在应用程序重新启动时自动重新打开。 I don't want that, since it interferes with debugging and legitimate crashes don't really happen with this app.我不希望那样,因为它会干扰调试,并且此应用程序不会真正发生合法崩溃。

Apple's NSDocument documentation contains nothing regarding the reopening of files, so I'm out of luck there. Apple 的 NSDocument 文档不包含任何关于重新打开文件的内容,所以我在那里运气不好。 Is there a proper way to do this?有没有正确的方法来做到这一点?

First create a subclass of NSDocumentController and make sure you create an instance of it in - (void)applicationWillFinishLaunching:(NSNotification *)notification so it becomes the sharedDocumentController.首先创建NSDocumentController的子类,并确保在- (void)applicationWillFinishLaunching:(NSNotification *)notification中创建它的实例,以便它成为 sharedDocumentController。 (see this question ) (见这个问题

Then in you subclass override the restore method:然后在你的子类中重写 restore 方法:

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler
{
    if (_preventDocumentRestoration) { // you need to decide when this var is true
        completionHandler(nil, [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);
    }
    else {
        [super restoreWindowWithIdentifier:identifier state:state completionHandler:completionHandler];
    }
}

Willeke的回答适用于XCode11。XCode-> Product-> Scheme-> Edit Scheme,然后选中复选框:启动应用程序而不进行状态还原

I have a read-only music player app for macOS that uses NSDocument to get all the file-handling logic for free.我有一个用于 macOS 的只读音乐播放器应用程序,它使用 NSDocument 免费获取所有文件处理逻辑。

The problem that I now have is that every time the app crashes (or is stopped by the debugger) while one or more player windows are open, they automatically get reopened when the app is restarted.我现在遇到的问题是,每次当一个或多个播放器窗口打开时应用程序崩溃(或被调试器停止),当应用程序重新启动时它们会自动重新打开。 I don't want that, since it interferes with debugging and legitimate crashes don't really happen with this app.我不希望那样,因为它会干扰调试,而且这个应用程序不会真正发生合法的崩溃。

Apple's NSDocument documentation contains nothing regarding the reopening of files, so I'm out of luck there. Apple 的 NSDocument 文档没有包含任何关于重新打开文件的内容,所以我在那里不走运。 Is there a proper way to do this?有没有合适的方法来做到这一点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM