core data - ios Magical Record save on UIApplicationWillTerminateNotification -


i have app stores system events core data database. perform saving use magicalrecord.

so, in logger class in init have:

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(handledidfinishlaunchingnotification) name:uiapplicationdidfinishlaunchingnotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(handlewillterminatenotification) name:uiapplicationwillterminatenotification object:nil]; 

in handle functions store simple entity text , timestamp property:

- (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];     }]; } 

when open , close (without crash) app few times, in db can see "did finish launching" entries (more one, i'm sure app closed, not moved bg), none of "will terminate".

i less surprised if launching event missed, because expect init method called after notification posted.

what can store terminate events?

i have answer:

it looks when application gets terminated, wont't let save in new background thread. saving in current thread works fine. had change saving method save in current thread, calling savewithblockandwait: instead of savewithblock:

- (void)handlewillterminatenotification {     [magicalrecord savewithblockandwait:^(nsmanagedobjectcontext *localcontext) {         dbmessage *dbmessage = [dbmessage createentityincontext:localcontext];         dbmessage.text = @"will terminate";         dbmessage.timestamp = [nsdate date];     }]; } 

now events succesfully saved on db.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -