Search code examples
iosobjective-ccllocationmanagernsnotificationcentergmsmapview

Is it valid to use post notification to track location updates


Can we use the 'post notification' for location updates to plot current location on google maps?? Or any better way to implement other than this ? Although i wish not to use KVO for @"Mylocations" in googlemaps.

In LocationTracker.m

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for(int i=0;i<locations.count;i++)
{
    CLLocation * newLocation = [locations objectAtIndex:i];
    CLLocationCoordinate2D theLocation = newLocation.coordinate;
    CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;

    [PlacesDetails sharedInstance].theLocation=theLocation;
    if(newLocation != nil && (!(theLocation.latitude == 0.0 && theLocation.longitude == 0.0)))
    {
        self.myLastLocation = theLocation;
        self.myLastLocationAccuracy= theAccuracy;
// Below implemented the post notification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLocation" object:nil];
    }
}
} 

In ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateGMSCameraPostition) name:@"updateLocation"       
object:nil];
}

-(void)updateGMSCameraPostition
{
  NSLog(@"CALLED UPDATELOCATION OBSERVER");
mapView_.camera = [GMSCameraPosition cameraWithTarget:[PlacesDetails sharedInstance].theLocation
                                                 zoom:14];}

Solution

  • Keban, That depends mainly how you have designed your app. You can use different pattern for this.

    1. NSNotificationCenter is also good, but you need to remove the observer on right time, if you don't do that then multiple notifications can fire and cause pathetic behaviour for your app.
    2. KVO, Willchangevalueforkey, you can also use that, but still you need to use that very carefully. This one is also good to measure the changes.
    3. Delegate Pattern, you can also use the delegate pattern for this.
    4. MVVM:- If you are using MVVM (Modal View-View Modal)designing pattern, then I would suggest you to go with blocks, this is one of my favourite way.

    For blocks you can have a look over these links.

    Blocks Apple Developer

    Blocks RyPress

    Inline Blocks

    I think it might help you.