(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.
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.
Use the Caching interface, which is implemented by the Cache class, to create an object to work with.
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.
cache.setIsPersistable(false);
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(); }
You can read entries from the cache method either locally, or from the server.
The ODataEntry class of the parser has two member properties. The getIsLocal() property checks if the particular entry is local, and getCachestate () checks if the local entry is created, updated, or deleted on the client. The cache states are represented by the cacheState{Updated,Deleted,Inserted} enum.
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(); }
try{ //Invoke the HTTP POST request ODataEntry entry = new ODataEntry(); entry.putPropertyValue("<property_name>", "<property_value>"); String postbody = Parser.buildODataEntryRequestBody(entry, "<collection_ID>", schema, IParser.FORMAT_XML); ILogger logger = new Logger(); ICache cache =new Cache(getApplicationContext(),logger); String generated_id =cache.addEntry(entry); //Invoke the HTTP POST request BaseRequest post = new BaseRequest(); post.setRequestMethod(BaseRequest.REQUEST_METHOD_POST); post.setData(postbody.getBytes()); reqMan.makeRequest(post); } catch(CacheException e){ Log.e("",e.getMessage(); }
try{ ILogger logger = new Logger(); ICache cache =new Cache(getApplicationContext(),logger); cache. initializeCache(); cache.updateEntry(entry); } catch(CacheException e){ Log.e("",e.getMessage(); }
try{ ILogger logger = new Logger(); ICache cache =new Cache(getApplicationContext(),logger); cache. initializeCache(); cache.deleteEntry(EntryID); } catch(CacheException e){ Log.e("",e.getMessage(); }
try{ ILogger logger = new Logger(); ICache cache =new Cache(getApplicationContext(),logger); cache. initializeCache(); cache.clearLocalEntry(<EntryID>); } catch(CacheException e){ Log.e("",e.getMessage();
//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(); }
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(); }
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(); }
The delta link retrieves the delta changes to a particular URL, since the last time URL was called.
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);