【发布时间】:2015-05-14 15:27:28
【问题描述】:
我有将系统事件存储到核心数据数据库的应用程序。为了执行保存,我使用 MagicalRecord。
所以,在init 的记录器类中,我有:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDidFinishLaunchingNotification) name:UIApplicationDidFinishLaunchingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleWillTerminateNotification) name:UIApplicationWillTerminateNotification object:nil];
在句柄函数中,我存储带有文本和时间戳属性的简单实体:
- (void)handleDidFinishLaunchingNotification
{
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
DBMessage *dbMessage = [DBMessage createEntityInContext:localContext];
dbMessage.text = @"Did finish launching";
dbMessage.timestamp = [NSDate date];
}];
}
- (void)handleWillTerminateNotification
{
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
DBMessage *dbMessage = [DBMessage createEntityInContext:localContext];
dbMessage.text = @"Will terminate";
dbMessage.timestamp = [NSDate date];
}];
}
当我几次打开和关闭(没有崩溃)应用程序时,在我的数据库中我可以看到“确实完成启动”条目(不止一个,所以我确定应用程序已关闭,不仅移动到 BG),而且没有一个“将终止”。
如果错过了启动事件,我不会那么惊讶,因为我可以预期在发布通知后会调用init 方法。
我可以做些什么来存储终止事件?
【问题讨论】:
标签: ios core-data magicalrecord