HERE iOS SDK Developer's Guide

Using CLE2 Offline

The same search requests that were possible via the server are also available offline. This CLE2 feature allows your app to pre-fetch full data layers from the server, or only cache objects returning from specific geospatial requests during runtime. This enables more efficient use of network resources, since your app can only cache and update data that is near the user's current location, or pre-download a whole layer only when a WiFi network becomes available. Also, because the data is local to the device, the application can become more responsive in offline mode, thus improving the user experience and user interface interactions.

The Offline CLE2 features are designed to be simple to use. This is so that all database synchronization and geospatial-related complexities are handled by the SDK, and you can focus on other parts of app development.

Populating the Local Storage

By default, offline features are disabled and the local storage contains no data. There are currently two ways to add geometries to make them available for offline searches:

  1. Enable caching when performing one or more requests (for example, using the NMACLE2ProximityRequest).
  2. Download one or more layers.

When caching is enabled in an NMACLE2Request, any returned geometries are automatically stored locally. To activate it, set the cacheEnabled property to YES before performing the request:


//Create any request as normal; In this example, we use the proximity request:

NMACLE2ProximityRequest * proximityRequest;
proximityRequest= [[NMACLE2ProximityRequest alloc] initWithLayer:@"HERE_SITES"
     center:[NMAGeoCoordinates geoCoordinatesWithLatitude:50.113905
        longitude:8.677608]
     radius:500]; // 500 meters

//activate caching:
[proximityRequest setCacheEnabled: YES];

//Now perform the request
[proximityRequest startWithBlock:^(NMACLE2Request *request, NMACLE2Result * result, NSError *error) {
  if(!error) {
    NSLog(@"Geometries returned in result.geometriesArray are now stored in the local database.");
  }
  }];

//now some geometries are in local storage. At a later point in time if we'd like to make an offline search,
//simply switch the requesMode to offline only in the request:
[proximityRequest setRequestMode: NMACLE2Offline];

//now calling a startWithBlock will operate completely offline:
[proximityRequest startWithBlock:^(NMACLE2Request *request, NMACLE2Result * result, NSError *error) {
  if(!error) {
    NSLog(@"Geometries returned in result.geometriesArray are now stored in the local database.");
  }
  }];

The second option is to use CLE2DataManager to insert data to the local storage using the downloadLayer method. CLE2DataManager is the central interaction point with the local storage is the object. With it, it is possible to:

  • Download a complete layer and all geometries in it from the server.
  • Check how many geometries are currently stored in total or in a specific layer.
  • Delete geometries belonging to a specific layer.
  • Purge the local storage.

The following is an example of how to use the downloadLayer method:


//Usage examples of NMACLE2DataManager

NMACLE2DataManager * dataManager = [NMACLE2DataManager sharedInstance];

//1 - Download a layer previously uploaded to the server
[storage downloadLayer:@"LOCAL_SHOPS" completionHandler:^(NSError * _Nullable error) {
      if(error)
      {
        NSLog(@"Unable to download layer. Error: %@", [error description]);
      }
      else
      {
        NSLog(@"Layers downloaded successfully.");
      }
    }];

//2 - Print the total number of stored geometries (sum of all downloaded layers),
//plus any cached geometries (e.g., from a proximity request with cache enabled)

NSNumber * numberOfGeometries;
[storage numberOfStoredGeometries: &numberOfGeometries];
NSLog(@"Total count: %lli", [numberOfStoredGeometries longLongValue]);

//3 - Delete all geometries from a specific layer
NSError * error = [[NMACLE2DataManager sharedManager] deleteLayer:@"POKEMONS"];
if(error)
{
   NSLog(@"Unable to delete layer. Error: %@", [error description]);
}
else
{
  NSLog(@"Layers deleted successfully.");
}

//4 - Completely delete all stored data
[[NMACLE2DataManager sharedManager] deleteAll];
Note: Since downloaded layers are not automatically updated when server data is updated, you need to download updates by calling downloadLayer: with the same layer ID again. When you call this method, it first checks if data updates are available, and then it downloads the data if it finds an update. Data updates are always performed through full layer downloads. Incremental downloads are not yet supported.

Automatic Mode

It is also possible to configure the search request to a "hybrid/automatic" mode, meaning that if during an online request the connection drops or there's a network error, the request will fall back to an offline operation automatically:


//Create any request as normal; In this example, we use the proximity request:

NMACLE2ProximityRequest * proximityRequest;
proximityRequest= [[NMACLE2ProximityRequest alloc] initWithLayer:@"HERE_SITES"
     center:[NMAGeoCoordinates geoCoordinatesWithLatitude:50.113905
        longitude:8.677608]
     radius:500]; // 500 meters


//Set to automatic mode
[proximityRequest setRequestMode: NMACLE2Automatic];


//now calling a startWithBlock will try an online request, if it fails the offline storage kicks in.
[proximityRequest startWithBlock:^(NMACLE2Request *request, NMACLE2Result * result, NSError *error) {
  if(!error) {
    NSLog(@"Geometries returned in result.geometriesArray.");
    //to find out whether this response came from the local storage or was
    //obtained from the server (online request), check the requestMode property:
    NMACLE2RequestMode * requestMode = result.requestMode;
  }
  }];

You cannot use this account to purchase a commercial plan on Developer Portal, as it is already associated to plans with different payment methods.

To purchase a commercial plan on Developer Portal, please register for or sign in with a different HERE Account.

Something took longer than expected.

The project should be available soon under your projects page.

Sorry, our services are not available in this region.

Something seems to have gone wrong. Please try again later.

We've detected that your account is set to Australian Dollars (AUD).
Unfortunately, we do not offer checkouts in AUD anymore.
You can continue using your current plan as normal, but to subscribe to one of our new plans,
please register for a new HERE account or contact us for billing questions on selfservesupport@here.com.