ios - Can we measure the data of M7/M8 while the iPhone is unawakened in the CLVisit delegate method? -
can write code in method m7's data , useful while don't run app?
-(void)locationmanager:(cllocationmanager *)manager didvisit:(clvisit*)visit { }
yes, can query cmmotionactivitymanager locationmanager:didvisit:
please note visits not reported app in real time, in tests delayed 20 60 minutes. means starting activity monitoring startactivityupdatestoqueue:withhandler: makes no sense, these updates won't tell happened during visit.
however still can fetch , analyze activity events happened during visit using queryactivitystartingfromdate:todate:toqueue:withhandler:
keep in mind locationmanager:didvisit: might , called while app in background mode, have 10 seconds query cmmotionactivitymanager , process data. since have no control on cmmotionactivitymanager , there no guarantee process query in timely fashion, may want invoke beginbackgroundtaskwithexpirationhandler: in locationmanager:didvisit: well.
@property (nonatomic) uibackgroundtaskidentifier bgtask; @property (nonatomic, strong) cmmotionactivitymanager *motionactivitymanager; ... - (void)locationmanager:(cllocationmanager *)manager didvisit:(clvisit *)visit { if (visit.arrivaldate && visit.departuredate) { // use strong self here, must end background task explicitly self.bgtask = [[uiapplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^ { [self stopbackgroundtask]; }]; [self.motionactivitymanager queryactivitystartingfromdate:visit.arrivaldate todate:visit.departuredate toqueue:[nsoperationqueue currentqueue] withhandler:^(nsarray *activities, nserror *error) { // handle cmmotionactivity history here [self stopbackgroundtask]; }]; } } - (void) stopbackgroundtask { if (self.bgtask != uibackgroundtaskinvalid) { [[uiapplication sharedapplication] endbackgroundtask:self.bgtask]; self.bgtask = uibackgroundtaskinvalid; } }
Comments
Post a Comment