gpt4 book ai didi

objective-c - 应用程序终止期间 NSDocument saveDocumentWithDelegate 死锁

转载 作者:行者123 更新时间:2023-12-04 04:59:05 25 4
gpt4 key购买 nike

NSDocument 仍然是软件维护的噩梦。

其他任何人都希望同步处理某些阻塞对话框的问题吗?

开始编辑:我可能已经找到了一个允许我同步等待的解决方案

任何人都可以验证这将是“Apple 批准”的解决方案吗?

static BOOL sWaitingForDidSaveModally = NO;
BOOL gWaitingForDidSaveCallback = NO; // NSDocument dialog calls didSave: when done



...
gWaitingForDidSaveCallback = true;
[toDocument saveDocumentWithDelegate:self
didSaveSelector:@selector(document:didSave:contextInfo:)
contextInfo:nil];
if ( gWaitingForDidSaveCallback )
{
// first, dispatch any other potential alerts synchronously
while ( gWaitingForDidSaveCallback && [NSApp modalWindow] )
[NSApp runModalForWindow: [NSApp modalWindow]];

if ( gWaitingForDidSaveCallback )
{
sWaitingForDidSaveModally = YES;
[NSApp runModalForWindow: [NSApp mbWindow]]; // mbWindow is our big (singleton) window
sWaitingForDidSaveModally = NO;
}
}
...


- (void)document:(NSDocument *)doc didSave:(BOOL)didSave contextInfo:(void *)contextInfo
{
[self recordLastSaveURL];
gWaitingForDidSaveCallback = NO;
if ( sWaitingForDidSaveModally )
[NSApp stopModal];

}

结束编辑

我要支持雪豹/狮子/ML

应用程序终止是一个丑陋的过程。
当用户决定退出,并且文档有需要保存的更改时,我称之为:
  gWaitingForDidSaveCallback = true;
[toDocument saveDocumentWithDelegate:self
didSaveSelector:@selector(document:didSave:contextInfo:)
contextInfo:nil];

我真的真的很希望这个调用是同步的,但是在最新的 Lion 中,这会挂起我的应用程序:
   while ( gWaitingForDidSaveCallback )
{
// didSave: callback clears sWaitingForDidSaveCallback
// do my own synchronous wait for now
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0.05]];
}

我对挂起的最佳猜测是 mouseDown: 窗口关闭按钮
正在混淆 NSDocument。

所以现在,我必须返回,并在我的应用程序主循环中添加不可维护的状态机逻辑,以防止用户执行各种危险的热键。

好吧,所以我笑着忍受它,然后又遇到了另一个障碍!

在以前的操作系统版本/SDK 中,[NSApp modalWindow] 会在它出现时返回一个窗口
处于这种状态。现在不行了!咕噜噜...
NSDocument 处于这种状态时没有 API 可以测试!

所以,现在没有机制来全局检查这个状态!
我必须向我的状态机添加另一个状态变量。

任何人都有针对此问题的更清晰的解决方案,适用于所有操作系统版本和所有当前(和 future )SDK?

最佳答案

更好的方法是将未保存的文档保存在链中。这很容易:

// Catch application terminate event
-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
NSDocumentController *dc = [NSDocumentController sharedDocumentController];
for (NSInteger i = 0; i < [[dc documents] count]; i++)
{
Document *doc = [[dc documents] objectAtIndex:i];
if ([doc isDocumentEdited])
{
// Save first unsaved document
[doc saveDocumentWithDelegate:self
didSaveSelector:@selector(document:didSave:contextInfo:)
contextInfo:(__bridge void *)([NSNumber numberWithInteger:i + 1])]; // Next document
return NSTerminateLater; // Wait until last document in chain will be saved
}
}
return NSTerminateNow; // All documents are saved or there are no open documents. Terminate.
}

...

// Document saving finished
-(void)document:(NSDocument *)doc didSave:(BOOL)didSave contextInfo:(void *)contextInfo
{
if (didSave) // Save button pressed
{
NSDocumentController *dc = [NSDocumentController sharedDocumentController];
NSInteger nextIndex = [(__bridge NSNumber *)contextInfo integerValue];
for (NSInteger i = nextIndex; i < [[dc documents] count]; i++)
{
Document *doc = [[dc documents] objectAtIndex:nextIndex];
if ([doc isDocumentEdited])
{
// Save next unsaved document
[doc saveDocumentWithDelegate:self
didSaveSelector:@selector(document:didSave:contextInfo:)
contextInfo:(__bridge void *)([NSNumber numberWithInteger:nextIndex + 1])]; // Next document
return;
}
}
[NSApp replyToApplicationShouldTerminate:YES]; // All documents saved. Terminate.
}
else [NSApp replyToApplicationShouldTerminate:NO]; // Saving canceled. Terminate canceled.

}

关于objective-c - 应用程序终止期间 NSDocument saveDocumentWithDelegate 死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16360898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com