ios 神奇的记录保存在 UIApplicationWillTerminateNotification 上
ios Magical Record save on UIApplicationWillTerminateNotification
我有将系统事件存储到核心数据数据库的应用程序。为了执行保存,我使用 MagicalRecord。
所以,在我的记录器 class 中 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];
}];
}
当我打开和关闭(没有崩溃)几次应用程序时,在我的数据库中我可以看到 "Did finish launching" 个条目(不止一个,所以我确定应用程序已关闭,而不仅仅是移动到 BG) , 但 none 共 "Will terminate".
如果错过启动事件,我不会感到惊讶,因为我可以预期 init
方法会在通知发布后被调用。
我可以做什么来存储终止事件?
我有一个答案:
看起来当应用程序终止时,不会让我保存在新的后台线程中。但是保存在当前线程中效果很好。所以我所要做的就是更改保存方法以保存在当前线程中,方法是调用 saveWithBlockAndWait:
而不是 saveWithBlock:
- (void)handleWillTerminateNotification
{
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
DBMessage *dbMessage = [DBMessage createEntityInContext:localContext];
dbMessage.text = @"Will terminate";
dbMessage.timestamp = [NSDate date];
}];
}
现在事件已成功保存到数据库中。
我有将系统事件存储到核心数据数据库的应用程序。为了执行保存,我使用 MagicalRecord。
所以,在我的记录器 class 中 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];
}];
}
当我打开和关闭(没有崩溃)几次应用程序时,在我的数据库中我可以看到 "Did finish launching" 个条目(不止一个,所以我确定应用程序已关闭,而不仅仅是移动到 BG) , 但 none 共 "Will terminate".
如果错过启动事件,我不会感到惊讶,因为我可以预期 init
方法会在通知发布后被调用。
我可以做什么来存储终止事件?
我有一个答案:
看起来当应用程序终止时,不会让我保存在新的后台线程中。但是保存在当前线程中效果很好。所以我所要做的就是更改保存方法以保存在当前线程中,方法是调用 saveWithBlockAndWait:
而不是 saveWithBlock:
- (void)handleWillTerminateNotification
{
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
DBMessage *dbMessage = [DBMessage createEntityInContext:localContext];
dbMessage.text = @"Will terminate";
dbMessage.timestamp = [NSDate date];
}];
}
现在事件已成功保存到数据库中。