0

I all I wonder if anybody can help me once again? I have moved my location coding from my View controller into an NSObject. I have then called this from the App Delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Updating Location
[Location sharedLocation];

//Timer for reloading the XML
recheckTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(recheckLocation) userInfo:nil repeats:YES];
return YES:
}

I have set a timer for when I want this process ran again

-(void)recheckLocation
{
  //Updating Location
  [Location sharedLocation];
  NSLog(@"Timer Triggered");
}

The only thing is when the timer triggers the sharedlocation does not get updated again?? Please can anybody help in advance? Many thanks, Jon.

#import "Location.h"

@implementation Location


@synthesize locationManager;


- (id)init {
  self = [super init];

  if(self) {
    self.locationManager = [CLLocationManager new];
    [self.locationManager setDelegate:self];
    [self.locationManager setDistanceFilter:500];//Metres
    [self.locationManager setHeadingFilter:90];
    [self.locationManager startMonitoringSignificantLocationChanges];
  }
  return self;
}


+ (Location*)sharedLocation {
  static Location* sharedLocation;
  if(!sharedLocation) {
    @synchronized(sharedLocation) {
      sharedLocation = [Location new];
    }
  }

  return sharedLocation;
}


//LOCATION CODING
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError' %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Failed to Get Your Current Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

 [errorAlert show];
}


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

  if (currentLocation != nil) {

    //Resolve Web Address
    webAddressResolved = [NSString stringWithFormat:@"XZYYFASDFA%f,%f.xml", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
    NSLog(@"Address Resolved %@", webAddressResolved);
  }

  //Stop Location Manager
  [locationManager stopMonitoringSignificantLocationChanges];

}
sosborn
  • 14,676
  • 2
  • 42
  • 46
jiffo
  • 7
  • 1
  • 5
  • I don't think the error is caused by Xcode. –  Apr 01 '13 at 19:37
  • What do you mean? by the iPhone? I don't get my error message about being unable to find my location?? – jiffo Apr 01 '13 at 19:48
  • @H2CO3 was making the point that you shouldn't title your post with "Xcode" if you're not asking about a feature or problem with Xcode (the IDE) itself. – Tim Apr 01 '13 at 22:41
  • why you want to call `[Location sharedLocation]` every 30s? and you have bug in it ([double ckecked locking](http://en.wikipedia.org/wiki/Double-checked_locking)). – Bryan Chen Apr 01 '13 at 23:53
  • Yes, I understand now Tim, my post title has been edited. – jiffo Apr 02 '13 at 06:38
  • Well xlc. I am using the location check to trigger code. I have to find the users location then it will go off and download some updates (XML) from another source (relevant to the users location) well that's the aim here. It does do that upon application launch but it fails to repeat itself after the timer triggers. 30 seconds was just a test. I'm afraid I don't understand your shared post on double checked locking?? Please can you explain a little more. Thanks, Jon. – jiffo Apr 02 '13 at 06:43

1 Answers1

0

Well what yo defined here is a singleton of Location and the purpose of shared location does not seem to update the location as it says in the code comments

what the sharedInstance does is return a reference that is once initialized and is used the same everywhere in the whole code.It gives you the location instance in return, which you are not retrieving anywhere and using it.You just call it but not use it.

Define a method to update location and call it after getting the memory from the location using

Location *sharedLoc=[Location sharedLocation];

and call the method to update the location as

[sharedLoc updateLocation];

Location.h

-(void)updateLocation;

in Location .m

-(void)updateLocation
{
//Code for updation purpose
}
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • thank you for the post but i've tried a couple of ways now and i can't seem to get the location manager to start to update the location? this is in the new void? – jiffo Apr 03 '13 at 17:44