Stalling at final test before challenges


#1

All done with chapter 5, and trying to make the final test. I simulate a location, and enter something in the txt field. But when i press done, it just stalls, hiding the field, and showing the activity indicator forever.

I have waited 20 mins with no result. However - i am running 1 mbit internet right now, at parents house, could that really be the cause?
(check somewhere else you might say, but thats not an option for a few days)

if not, my WhereamiViewController.m file is below.

Thanks in advance. Anders

[code]//
// WhereamiViewController.m
// Whereami
//
// Created by Anders on 09/05/13.
// Copyright © 2013 Anders. All rights reserved.
//

#import “WhereamiViewController.h”
#import “BNRMapPoint.h”

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController

  • (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if(self){
    //create location manaer object
    locationManager = [[CLLocationManager alloc]init];

      [locationManager setDelegate:self];
    
      
      //and we want it to be as accurate as possible regardles of power/time consumption
      [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    

    }
    return self;
    }

-(void) findLocation;
{
[locationManager startUpdatingLocation];
[activityIndicator startAnimating];
[locationTitleField setHidden:YES];
}

-(void) foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = [loc coordinate];

//create an instance of BNRMappoint with the current data
BNRMapPoint *mp = [[BNRMapPoint alloc ]initWithCoordinate:coord title:[locationTitleField text]];

//add it to the map view
[worldView addAnnotation:mp];

//zoom the region to this location
MKCoordinateRegion region =  MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[worldView setRegion:region];

//reset the ui
[locationTitleField setText:@""];
[activityIndicator startAnimating];
[locationTitleField setHidden:NO];
[locationManager stopUpdatingLocation];

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];
}

-(void)viewDidLoad
{
[worldView setShowsUserLocation:YES];
[worldView setMapType:MKMapTypeSatellite];
}

-(void)dealloc
{
// tell the locationManager to stop sending us messages
[locationManager setDelegate:nil];
}

  • (void) locationManager:(CLLocationManager *) manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"%@",newLocation);

    //How many seconds ago was this new location created
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    //CLLocationmanagers wil return the last found location of the device first,
    // you dont want that data in this case
    //if this location was mad more than 3 minutes ago, ignore it.
    if(t<180){
    //this is cached data, you dont want it, keep looking
    return;
    }

    [self foundLocation:newLocation];
    }

-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@“Could not find location: %@”, error);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self findLocation];

[textField resignFirstResponder];

return YES;

}
@end
[/code]


#2

I fell foul of this today, but adding a break point on the return line resolved it.

It’s a problem in the Location Manage delegate code.
Your code reads:-

if(t<180){ //this is cached data, you dont want it, keep looking return; }

The time interval is always a negative number and so will always be below 180. You need to check against -180

this will work correctly.

if(t < -180){ //this is cached data, you dont want it, keep looking return; }