Objective c 如何快速获取应用程序终止通知

Objective c 如何快速获取应用程序终止通知,objective-c,cocoa,notifications,Objective C,Cocoa,Notifications,要获得应用程序终止通知,我有如下内容 NSNotificationCenter* center = [[NSWorkspace sharedWorkspace] notificationCenter]; [center addObserver:self selector:@selector(appTerminated:) name:NSWorkspaceDidTerminateApplicationNotifi

要获得应用程序终止通知,我有如下内容

 NSNotificationCenter*  center = [[NSWorkspace sharedWorkspace] notificationCenter];

    [center addObserver:self 
               selector:@selector(appTerminated:) 
                   name:NSWorkspaceDidTerminateApplicationNotification 
                 object:nil
     ];




- (void)appTerminated:(NSNotification *)note
{
    NSLog(@"+ appTerminated");
}
实际上我关心的是当firefox应用程序退出/重新启动时,我需要更新其数据库。当firefox手动退出时,我可以借助appTerminated更新,因为firefox释放了对数据库的锁定。当它处于运行状态时,我无法更新数据库,因为firefox正在锁定它。当firefox重新启动时,它退出和重新启动的速度太快,因此我无法更新数据库,因为它处于运行状态。我需要在重新启动之前更新数据库,即当firefox处于退出状态时

因此,我需要在firefox即将退出之前发出通知。 是否有任何api可用于此,或者请给出一些想法


提前感谢

我想您有两个应用程序,一个监视另一个。你的担心似乎是,在观察者完成工作之前,你不希望被观察的应用程序真正开始做任何事情

在这种情况下,您只需要在流程之间进行通信。被监视的应用程序应该等到监视程序完成其工作。您可以使用锁来实现这一点,也可以使用
NSDistributedNotification
(或其他IPC机制)将消息从观察者发送到被观察者,让其知道它可能会继续


我更喜欢锁定机制,因为如果观察者失败,它会正常工作。最正确的锁定位置应该是数据库,因为这是您试图保护的资源。

我会尝试这样做:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    if (isMyDatabaseClosed) {
         [self closeMyDatabaseAndQuit];
         return NSTerminateLater;
    } else {
         return NSTerminateNow;
    }
}

-(void)closeMyDatabaseAndQuit 
{
/* close your database, etc...*/
    [NSApp replyToApplicationShouldTerminate: YES];
}

代码没有经过测试,但您应该有一个想法。

您使用的是核心数据吗?