0

I have an iphone app that logs data points every time the gps gets a new location point.

I have enabled the "Required background modes" "App registers for location updates" bool in my APP_NAME-info.plist however the background service seams to stop logging, for a reason I cannot associate with anything I am doing other than the device have not been touched for some time. is there something I haven't thought of? I supply my code in case that is at fault.

EDIT I am constantly moving while testing so the gps should be updating.

any pointers in the right direction would be very gratefully received.

  -(void)locationUpdate:(CLLocation *)location
{
    BOOL sc=YES;
    if(!sc){
        UIAlertView *al=[[UIAlertView alloc]initWithTitle:@"you have not got gps enabled" message:@"if you dont allow us access to the gps the this app cant log anything" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil] ;
        [al show];
        [al release];
    }

    if (location != nil) {
         gpscylces++;        
       // NSDate* sourceDate = [NSDate date];
        //NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        //NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //[formatter setTimeZone:timeZone];
        //[formatter setDateFormat:@"EEE MMM dd HH:mm:ss zzz yyyy"];

       // NSString *strGMTDate = [formatter stringFromDate:sourceDate];
       // NSLog(@"date : %@", strGMTDate);

        CLLocationCoordinate2D userCoordi = [location coordinate];
        NSString *lat = [NSString stringWithFormat:@"%f",userCoordi.latitude];
        currLat = userCoordi.latitude;
        NSLog(@"lat : %@", lat);
        NSString *log = [NSString stringWithFormat:@"%f",userCoordi.longitude];
        currLog = userCoordi.longitude;
        NSLog(@"log : %@", log);
        currAlt=location.altitude;
        NSDate *date =[NSDate date];
        currTime= [date timeIntervalSince1970];
        int t=(int)currTime;

        NSLog(@"date : %d", t);
        NSString *accuracy = [NSString stringWithFormat:@"%f",location.horizontalAccuracy];
        NSLog(@"accuracy : %@", accuracy);
        NSString *speed = [NSString stringWithFormat:@"%f",[location speed]];
        NSLog(@"speed : %@", speed);
        if (currLat==0&&currLog==0) {
            gpscylces++;
        }
        if(location.horizontalAccuracy>100){
            NSLog(@"----not accurate enaough");
            return;}
        if((!staticshooting)|(staticshooting &(gpscylces==3))){
        [[G4EMADBModel sharedInstance] openDB];
        NSString *strSQL = [NSString stringWithFormat:@"INSERT INTO TRIP_DATA (TRIPID,GMTTIMESTAMP,LATITUDE,LONGITUDE,ALTITUDE,ACCURACY,SPEED, SPECIES, KILLCOUNT) VALUES(%i,%i, %f, %f,%f,%@,%@, NULL, NULL)",currentTripId,t,currLat,currLog,currAlt, accuracy,speed];
            if (waitingForGPS) {
               startlogbtn.titleLabel.text=@"Stop Logging";
                waitingForGPS=NO;
            } 

            if(lastSyncTime+300<currTime)
            {
                 lastSyncTime=currTime;
                [self updateLocationToServer];
            }
            else{NSLog(@"we have synced this last syntime is %f and curtime is %f ",lastSyncTime,currTime);}

        NSLog(@"strQuery : %@", strSQL);
        if (staticshooting) {[CLController.locMgr stopUpdatingLocation];}
        BOOL success=[[G4EMADBModel sharedInstance] executeUpdate:strSQL];
       // [formatter autorelease];
        if (success) {
            NSLog(@"Success");}else{
                NSLog(@"database fail");
        }
        }//static shooting if
    }
}

-(void)locationError:(NSError *)error {

    NSLog(@"Error : %@", [error description]);
}
Mark Gilchrist
  • 1,972
  • 3
  • 24
  • 44
  • Stupid question - but if the device hasn't been touched for a while, could it be that the GPS isn't updating? – Floris Sep 13 '13 at 14:38
  • I am, moving so the gps should be updating – Mark Gilchrist Sep 13 '13 at 14:45
  • OK - I see you have updated your question. Does this happen when the device goes into standby mode - in other words, it works OK when you first start your application? And does it come back to life once the phone wakes up - or is the background app indeed "killed" as your title suggests. – Floris Sep 13 '13 at 14:52
  • i start it it works fine, I can push the power button, put the phone in my pocket and walk around, it works fine. I can restart the app, by pushing he power button and unlocking the key board, it works fine, but then for reasons I can understand it will stop logging. weather i have left it in background for too long I cannot say – Mark Gilchrist Sep 13 '13 at 14:56
  • Is something limiting the size of your database? How large does it get when it stops logging - it is always the same size? – Floris Sep 13 '13 at 14:58
  • the database is pretty small, in fact I recently wiped the app and reinstalled it for different reasons, and the db ATM hold just one trip, (about 1k) and the problem is as prevalent now as it was before – Mark Gilchrist Sep 13 '13 at 15:05
  • Could [this answer](http://stackoverflow.com/a/6653110/1967396) be of any help for you? – Floris Sep 13 '13 at 15:12
  • I have looked at my code, in the -aplicationDidEnterBackGround there is nothing but comments, so it is likely this is a cause, however the program is running for much longer than 5 seconds without being killed, more like 20 mins – Mark Gilchrist Sep 13 '13 at 15:22
  • Maybe it (the background task) returns within 5 seconds most of the time... and then one time it doesn't. That kind of thing can happen and would explain why it "eventually gets killed", but not right away. – Floris Sep 13 '13 at 15:55
  • it apears that i ceratinly need some code in the aplicationWillEnterBackgroud I am just try to get that right atm – Mark Gilchrist Sep 13 '13 at 16:05

0 Answers0