Caching the Data

(Optional, offline mode only) Caching is an important part of getting an application to work in offline mode. Use the Cache API to cache and persist data, when the mobile device is out of the network range and process the data later when the device within the range of the network.

Note:
  • Due to limitation on the emulator, you cannot determine the network connection state. For more information on other limitations, see Emulator Limitations in Using the Emulator at the Android Developer web site.
  • The delete tombstones for delta query support is not available on OData JSON format.

The Cache API is divided into two parts, one that stores a copy of the entries that you receive from the server called as "server copy " and the other is locally modified entries called as "local copy". You can also use this procedure for online scenarios.

Prerequisites

Use the Caching interface, which is implemented by the Cache class, to create an object to work with.

Initialize the Cache

To initialize all the elements required for a cache to work, use the initializeCache method:

try{
      	ILogger logger = new Logger();
	ICache cache =new Cache(getApplicationContext(),logger);
	cache.initializeCache();
	 
   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

Cache can be used in persistable mode (database) and non-persistable mode (memory). By default, cache is persistable.

If you do not want to persist cache to the database, use the following API:
cache.setIsPersistable(false);

Merge to Cache

To incrementally update the cache for server entries with the latest state of server objects, use the mergeEntries method. Every online HTTP GET operation on a URL should be followed by a MergeEntries method to enable the server cache to be in-sync with the backend service. It is important to instate a server cache using the MergeEntries method for all operations to work after a successful online GET request for a URL at the initial stage. It is recommended to optimize GET operation with Delta support to avoid redundancies.

try{
	 ILogger logger = new Logger();
Parser parser = new Parser(pref, logger);
	 ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
IODataFeed feed = parser.parseODataFeed(entriesdoc, collectionID, schema);
	cache.MergeEntries(feed, EntryURL);parameters.
   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

Note: There is no overhead with the cache capabilities, when the parsed content is delivered for presentation, even before merging on to the cache, and when the merging operation is invoked on another thread. So, there is no wait time until the cache is being written.

Read Entry from Cache

You can read entries from the cache method either locally, or from the server.

You need to combine the server and local entries to display the latest version of data on your application.

/*Read entry from server.*/
try{
	 ILogger logger = new Logger();
	 ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
	 List<IODataEntry> entries = cache.ReadEntriesServer(urlKey); // return all entries respective to the parameter.
   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

/*Read entry from local*/
try{
	 ILogger logger = new Logger();
	 ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
	 List<IODataEntry> entries = cache.ReadEntriesLocal(CollectionID,EntryID); // return all entries respective to  either of the 	parameters.
   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

Managing Locally Modified Entries in Cache

Store Document in Cache

To cache the service and metadata document (schema) for a particular service URL, use the storeDocument method . You can also define your own enum type and store the data for that enum type. The service document must be stored only after the metadata document is parsed.
//Service document
try{
	 ILogger logger = new Logger();
	 ICache cache =new Cache(getApplicationContext(),logger);
cache. initializeCache();
	 cache.storeDocument(serviceDocumentObject, DocumentType.ServiceDocument, serviceDocUrl);
   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

//MetaData Document
try{
	 ILogger logger = new Logger();
	 ICache cache =new Cache(getApplicationContext(),logger);
	cache.initializeCache();
	cache.storeDocument(serviceDocumentObject, DocumentType.MetaDocument, serviceDocUrl);
}
catch(CacheException e){
	Log.e("",e.getMessage();
}

Read Document from Cache

To read the service and metadata documents from cache, use the readDocumentForUrlKey method. This method returns the stored document in the cache. The application should pass the document type and the URL for which the document is relevant.

try{
	 ILogger logger = new Logger();
	 ICache cache =new Cache(getApplicationContext(),logger);
     cache.initializeCache();
	 IODataServiceDocument serviceDocumentObject = (IODataServiceDocument )cache.readDocument(DocumentType.ServiceDocument, serviceDocUrl);
	 IODataSchema metadata = (IODataSchema)cache.readDocument(DocumentType.MetaDocument, serviceDocUrl);
   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

Clearing the Cache

Use the clearCache method to clear the cache and persistence of all the entries based on URL key. This method also deletes the delta token of any document stored against this URL. Hence it is recommended that you use this method only when the application does not support delta queries.

try{
	 ILogger logger = new Logger();
	 ICache cache =new Cache(getApplicationContext(),logger);
cache.initializeCache();
	 cache.clearCache(URLKey);

   }
catch(CacheException e){
	Log.e("",e.getMessage();
}

Using Delta Link

The delta link retrieves the delta changes to a particular URL, since the last time URL was called.

Note: You must pass the complete request URL as urlKey. For example, cache().mergeEntries(list, aRequest.getRequestUrl()).

The default option in cache is to get the delta link if OData entry supports delta link. In this case, once mergeEntries API of cache is called to save server entries into cache, the next GET the application fires, will internally be fired for delta link for the OData entry. So only delta changes will be available as the response and the application developer can call mergeEntries to merge the entries in the cache.

The application developer can disable default delta link handling by calling the following API:

requestObject.disableDeltaHandling(true);
Related concepts
Offline OData Support