0

I have 2 classes as NSObject subclasses:

1st class is more likely act as adapter. It send data to Class2 for process async task. When delegaton fired I would like to post back data to adaptor class.

In adaptor class:

Class2 *cls = [[Class2 alloc] init];
[ftc fetchLocation];

In Class2.m

-(void)fetchLocation{

    if(IS_OS_8_OR_LATER) {
        [self.locationManager requestAlwaysAuthorization];
    }

    self.locationManager = [[CLLocationManager alloc] init];

    if ([CLLocationManager locationServicesEnabled]){
        NSLog(@"Enable");
    }
    self.locationManager.delegate =self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
    [self.locationManager startUpdatingLocation];
}

When I call fetch-location method from adaptor, it really calls and reads lines, but after that, Class2 disappears and gone back to Adapter class without waiting delegation (didUpdateLocations)

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    NSString *latitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude];
    NSString *longtitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude];
    NSString *altitude = [NSString stringWithFormat:@"%f",self.locationManager.location.altitude];
    NSString *speed = [NSString stringWithFormat:@"%f",self.locationManager.location.speed];

    NSDictionary *locationDictionary = @{@"latitude":latitude,@"longtitude":longtitude,@"altitude":altitude,@"speed":speed};    
    if (locations.count >0 && [locations isKindOfClass:[NSArray class]]) {
        [self.delegate userLocationHasUpdated:self :locationDictionary];
        [self.locationManager stopUpdatingLocation];
        self.locationManager = nil;
        return;
    }
}

But if I just run Class2 and remove adapter from compile (with first initialiser) it runs as expected, How can I achieve to handle delegation methods from another class that fired ?

Best Regards.

dasdom
  • 13,975
  • 2
  • 47
  • 58
Onder OZCAN
  • 1,556
  • 1
  • 16
  • 36

2 Answers2

1

You have several options, really. One could be making your second class a property of your first class (singleton pattern would fit nice here, I guess). Then you can either declare a protocol in your second class and notify your first class via delegate methods (non-singleton implementation) or use NSNotificationCenter to post a notification (singleton implementation).

The second option would be to pass a block with completion handler to the second class. You could declare your method in the second class like this, for example (adjust return type and arguments of the block, if needed):

- (void)updateLocationWithCompletionHandler:(void (^)(void))completion;

Implement it so that you call the completion block after you get the geolocation update results.

And call it from the first class like:

[self.secondClass updateLocationWithCompletionHandler:^
      {
          // Your completion code here.
      }];

Hope this helps (sorry, didn't check the code in Xcode, get rid of typos, if any).

Daniil Korotin
  • 720
  • 3
  • 14
1

Possible duplicate of iOS CLLocationManager in a separate class. You have to create a singleton class for to get the location if you want to have seperate class for handing the location manager. You will find the guidance from the shared link

Community
  • 1
  • 1
Ashwin P
  • 501
  • 1
  • 3
  • 19