Question

I have a Location.m model, which I use to obtain latitude and longitude. When I have those, I call my delegate method which is implemented in my Foursquare.m model.

Location.h looks like this:

@class Location;

@protocol LocationDelegate <NSObject>
- (void)location:(Location *)loc didFinishFindingLocation:(CLLocation *)location;
@end

@interface Location : NSObject <CLLocationManagerDelegate>

@property (nonatomic, weak) id <LocationDelegate> delegate;    
- (void)startLocationManager;

@end

Location.m:

@implementation Location
{
    CLLocationManager *_locationManager;
    BOOL _locationIsUpdated;
}

- (void)startLocationManager
{
    NSLog(@"In startLocationManager");
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    _locationManager.delegate = self;
    _locationIsUpdated = NO;
    [_locationManager startUpdatingLocation];
}

#pragma mark - LocationManager Delegates

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (locations && _locationIsUpdated == NO) {
        self.location = [locations lastObject];
        NSLog(@"%@", _location);
        _locationIsUpdated = YES;
        [self.delegate location:self didFinishFindingLocation:self.location];
    }

}

@end

When the delegate method is called from the Foursquare model, and I NSLog out the response in an array (called self.dataResponse). It always outputs (null) first, then all the JSON. Why does that happen? If I NSLog out dataDictionary (see code below) directly, the JSON output shows a error-code:400 and nothing is printed. Couple of milliseconds after everything is printed and seems to be working fine...

Here is a picture: When I log the dataDictionary enter image description here

Here is the delegate method implemented in Foursquare.m:

- (void)location:(Location *)loc didFinishFindingLocation:(CLLocation *)location
{
    loc.delegate = self;

    self.resultFromResponse = [[NSMutableArray alloc] init];
    NSString *search = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%f,%f&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=%@&client_secret=%@&v=%@", location.coordinate.latitude, location.coordinate.longitude, kFourdquareAPIKey, fFoursquareSecret, [Foursquare getCurrentDate]];

    NSURL *url = [NSURL URLWithString:search];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *dataDictionary = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (!error) {
            self.dataResponse = dataDictionary[@"response"][@"venues"];
            NSLog(@"%@", self.dataResponse);
            for (NSDictionary *venue in self.dataResponse) {
                FSDataFromJSON *data = [[FSDataFromJSON alloc] init];
                data.name = venue[@"name"];
                data.phone = venue[@"phone"];
                data.location = venue[@"location"];
                [self.resultFromResponse addObject:data];
            }
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"JSONRequestFinishedLoading" object:nil];
        } else {
            NSLog(@"%@", error.description);
        }
    }];

    [task resume];
}

At the very end, these models is started in my MainViewController:

 _location = [[Location alloc] init];
    [_location startLocationManager];

    _foursquare = [[Foursquare alloc] init];
    [_foursquare location:_location didFinishFindingLocation:_location.location];
Was it helpful?

Solution

From what I can tell, the last line of code in your question is likely the problem:

[_foursquare location:_location didFinishFindingLocation:_location.location];

You shouldn't be calling your delegate method explicitly (before the location has been set). Instead, you should be waiting for the CLLocation callback to kick it off.

Other thoughts:

  • If the Location class has a .location property, I think that's confusing.

  • Location.m is not a model, it's the file containing the class implementation. I'd say the Location class is a model.

  • You could simplify your delegate method as didFindLocation:(Location *)location since you can get the CLLocation in the method as location.location.

  • What are the three most important factors in real estate? location.location.location!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top