这将进入一个无限循环,我不知道为什么

时间:2013-12-12 20:19:00

标签: ios objective-c cllocationmanager

知道为什么会进入无限循环:

我刚刚完成了这个应用程序,它正在工作,然后我停留在locationManager中的for循环中,希望看到用户想要运行多少次迭代,它似乎不起作用,我得到的所有输出是迭代和延迟记录以及“在主循环中”语句。当我拿出for循环时它似乎没有帮助。很抱歉代码看起来很糟糕,它有点匆忙,我没有时间清理它。

- (void)viewDidLoad
{
    [super viewDidLoad];

//NSLog(@"Checking whether location services are available...");
    if ([CLLocationManager locationServicesEnabled])
    {
    NSLog(@"Location services are enabled.\n\n");
    }
    else
    {
    NSLog(@"Location services are not enabled\n\n");
    }

    [self.getLocationButton setTitle:@"Start Logging" forState:UIControlStateNormal];
    [self.getLocationButton setTitle:@"Waiting for location..." forState:UIControlStateDisabled];
    [self.getLocationButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];


    dateFormatterForGpsTimestamps = [[NSDateFormatter alloc] init];
    if (dateFormatterForGpsTimestamps)
    {
        [dateFormatterForGpsTimestamps setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS zzz"];
    }

    self.locationManager = [[CLLocationManager alloc] init];
    NSLog(@"allocate locationManager");
    if (self.locationManager)
    {
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        NSLog(@"set locationManager delegate");
    }

    self.locationRequestTimestamp = nil;
    self.locationReceiptTimestamp = nil;

    numberFormatter = [[NSNumberFormatter alloc] init];
}

- (IBAction)getLocation:(id)sender
{
    // Reset timer
    self.locationRequestTimestamp = nil;
    self.locationReceiptTimestamp = nil;
    NSLog(@"in getLocation function");


    [self.getLocationButton setEnabled:(NO)];

    if (self.getLocationButton.isEnabled == 1){
        NSLog(@"Button is still enabled");
        return;
    }
    else {
        NSLog(@"in the else statement before for loop");
        //DDLogError(@"get location button disabled.  moving into location manager");
        [self.locationManager startUpdatingLocation];

    }
    //[self.locationManager stopUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // Get delay and iteration variable from input
    int delays = [self.delay.text intValue];
    int iterations = [self.iteration.text intValue];
    NSLog(@"iterations: %d delays: %d", iterations, delays);

    for (int i = 0; i < iterations; ++i){

        self.locationRequestTimestamp = [NSDate date];
        NSLog(@"in the main loop");
        // Stop the timer


        CLLocation *location = [locations lastObject];

        NSDate *timestamp = location.timestamp;

        // Reject the location if it was determined before the request was made
        if ([timestamp compare:self.locationRequestTimestamp] == NSOrderedAscending)
        {
            return;
        }

        //[self.locationManager stopUpdatingLocation];

        self.timestamp.text = [dateFormatterForGpsTimestamps stringFromDate:timestamp];
        self.latitude.text  = [[NSString alloc] initWithFormat:@"%.6f", location.coordinate.latitude];
        self.longitude.text = [[NSString alloc] initWithFormat:@"%.6f", location.coordinate.longitude];
        self.estimatedPositionError.text = [[NSString alloc] initWithFormat:@"± %.3f m", location.horizontalAccuracy];

        self.locationReceiptTimestamp = [NSDate date];

    // Calculate TTFF using start and end times

        self.ttff.text = [[NSString alloc] initWithFormat:@"%f s", [self.locationReceiptTimestamp timeIntervalSinceDate:self.locationRequestTimestamp]];

    // Calculate error from known position
        NSNumber* knownLat  = [numberFormatter numberFromString:self.knownLatBox.text];
        NSNumber* knownLong = [numberFormatter numberFromString:self.knownLongBox.text];

        CLLocation *knownLocation = nil;
        CLLocationDistance accuracy = -1;

        if (knownLat && knownLong)
        {
            knownLocation = [[CLLocation alloc] initWithLatitude:[knownLat doubleValue] longitude:[knownLong doubleValue]];
        //NSLog(@"Distance from known location: %f", [location distanceFromLocation:knownLocation]);
        }
        else
        {
            //NSLog(@"Problem calculating position error");
        }

        if (knownLocation)
        {
            accuracy = [location distanceFromLocation:knownLocation];
            self.accuracyLabel.text = [[NSString alloc] initWithFormat:@"± %.3f m", accuracy];
        }
        else
        {
            self.accuracyLabel.text = @"Unknown";
        }

        [self.getLocationButton setEnabled:YES];

    // Add location to log box
        CLLocationCoordinate2D coordinate = location.coordinate;

        CLLocationDegrees  lat = coordinate.latitude;
        CLLocationDegrees  lon = coordinate.longitude;
        CLLocationAccuracy epe = location.horizontalAccuracy;

        NSString *locationString = [[NSString alloc] initWithFormat:
                                /* lat */   @"%.6f, "
                                /* long */    "%.6f, "
                                /* epe */    "%.3f m, "
                                /* ttff */    "%@, "
                                /* accuracy */    "%.3f m\n",
                                lat, lon, epe, self.ttff.text, accuracy];

        self.logBox.text = [[NSString alloc] initWithFormat:@"%@%@", self.logBox.text, locationString];
        NSLog(@"%@", locationString);

        sleep(delays);
    //self.locationRequestTimestamp = [NSDate date];
    }
NSLog(@"left the for loop");
[self.locationManager stopUpdatingLocation];
}

0 个答案:

没有答案
相关问题