0

As my user changes location, I need to compare his location with an array of locations I pull from the web. However, in my appDelegate, I'm not sure where exactly to place my code as I'm not sure what methods are called or not called when the app is terminated, but the CLLocationManager still works.

Specifically, I need to input this code where it will actually be called when the app is still terminated:

// alloc and init the various (Mutable)Array properties
self.locations = [[NSMutableArray alloc] init];

// Create new HomeModel object and assign it to _homeModel variable
_homeModel = [[DealsModel alloc] init];

// Set this view controller object as the delegate for the home model object
_homeModel.delegate = self;

// Call the download items method of the home model object
[_homeModel downloadItems];

The _homeModel will then call this method:

-(void)itemsDownloaded:(NSArray *)items
{
    // This delegate method will get called when the items are finished downloading

    // Set the downloaded items to the array
    _locations = [items copy];
}

Which I will further edit to compare the user's location to the array of locations.

The thing is, this array of locations only changes once a week. Does the app really have to pull it from the web every time the user's location changes? Or is there a way to cache this and only pull it when self.locations has been deallocated?

This is what I have now, but I feel there must be a better way:

@interface AppDelegate () <CLLocationManagerDelegate>
{
    DealsModel *_homeModel;
}

@property BOOL didRunBefore;
@property CLLocationManager *locationManager;
@property NSMutableArray *deals;

@end

@implementation AppDelegate

-(void)itemsDownloaded:(NSArray *)items
{
    // This delegate method will get called when the items are finished downloading

    // Set the downloaded items to the array
    _deals = [items copy];
    [self compareSponsorLocations:_deals toUserLocation:[self.locationManager location]];
}

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

    if (!self.deals) {
    // alloc and init the various (Mutable)Array properties
    self.deals = [[NSMutableArray alloc] init];

    // Create new HomeModel object and assign it to _homeModel variable
    _homeModel = [[DealsModel alloc] init];

    // Set this view controller object as the delegate for the home model object
    _homeModel.delegate = self;

    // Call the download items method of the home model object
    [_homeModel downloadItems];
    } else {
    [self compareSponsorLocations:self.deals toUserLocation:[locations lastObject]];
    }
}

- (void) compareSponsorLocations: (NSArray *) array toUserLocation: (CLLocation *) location
{
    for (Deal *deal in array) {
        NSLog(@"%@", deal.name);
    }
    NSLog(@"%@", location.description);
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
Jameson
  • 4,198
  • 4
  • 17
  • 31

1 Answers1

1

To handle location updates when you app is in background or even terminated, you can use "Significant Location Changes". It will trigger application to start in background mode when location has been changed significantly. Then you can start a background task to perform the operations on your need.

Documentation: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW8

Easy way to simulate it and debug: XCode / iOS simulator: Trigger significant location change manually

Community
  • 1
  • 1
Krivoblotsky
  • 1,492
  • 11
  • 17