I'm using Rest
to make a call to a remote server that I have no access to. I want to cache received data permanently for offline use without checking Last-Modified or ETag in the headers.
I expected CachingMode.MANUAL
mechanism to check if there's an offline content and if there's none, then go online to fetch the content, but it doesn't.
To circumvent this, I had to first use Rest
with CachingMode.OFFLINE
and if that returns 404, then make another call with CachingMode.SMART
.
Shouldn't there be an option of (let's say CachingMode.OFFLINE_FIRST
) that checks offline first and if no content then goes online with (CachingMode.SMART
)?
Below is my current approach:
Response<Map> response = Rest.get(url)
.cacheMode(CachingMode.OFFLINE)
.queryParam("param", value)
.jsonContent().onErrorCodeJSON(e -> {
throw new RuntimeException(createErrorMessage(e));
}).onError(e -> {
if (e.getResponseCode() == 0 || e.getConnectionRequest().getResponseCode() == 404) {
is404 = true;
return;
}
throw new RuntimeException("Network error. Please check your connection and try again.");
}).timeout(6000).getAsJsonMap();
if (is404) {
is404 = false;
response = Rest.get(url)
.cacheMode(CachingMode.SMART)
.queryParam("param", value)
.jsonContent().onErrorCodeJSON(e -> {
throw new RuntimeException(createErrorMessage(e));
}).onError(e -> {
throw new RuntimeException("Network error. Please check your connection and try again.");
}).timeout(6000).getAsJsonMap();
}
This makes sense. Added support for this in this commit: https://github.com/codenameone/CodenameOne/commit/fd81d979507fb08ee1d595b94df5973b322766a3