From 429b56d5ef65b5efbe3237708bd475487fcc3028 Mon Sep 17 00:00:00 2001 From: Tanner Semerad Date: Sun, 31 May 2020 13:05:08 -0700 Subject: [PATCH] add invalidateAmbientCache --- .../rctmgl/modules/RCTMGLOfflineModule.java | 18 ++++++++++++++++++ docs/OfflineManager.md | 16 ++++++++++++++++ docs/docs.json | 14 ++++++++++++++ ios/RCTMGL/MGLOfflineModule.m | 11 +++++++++++ javascript/modules/offline/offlineManager.js | 16 ++++++++++++++++ 5 files changed, 75 insertions(+) diff --git a/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLOfflineModule.java b/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLOfflineModule.java index 4679d8412..91f245534 100644 --- a/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLOfflineModule.java +++ b/android/rctmgl/src/main/java/com/mapbox/rctmgl/modules/RCTMGLOfflineModule.java @@ -121,6 +121,24 @@ public void onError(String error) { }); } + @ReactMethod + public void invalidateAmbientCache(final Promise promise) { + activateFileSource(); + final OfflineManager offlineManager = OfflineManager.getInstance(mReactContext); + offlineManager.invalidateAmbientCache(new OfflineManager.FileSourceCallback() { + @Override + public void onSuccess() { + promise.resolve(null); + } + + @Override + public void onError(String error) { + promise.reject("invalidateAmbientCache", error); + } + }); + } + + @ReactMethod public void resetDatabase(final Promise promise) { activateFileSource(); diff --git a/docs/OfflineManager.md b/docs/OfflineManager.md index b1c31e581..b49307eb9 100644 --- a/docs/OfflineManager.md +++ b/docs/OfflineManager.md @@ -47,6 +47,22 @@ await MapboxGL.offlineManager.deletePack('packName') ``` +#### invalidateAmbientCache() + +Forces a revalidation of the tiles in the ambient cache and downloads a fresh version of the tiles from the tile server.
This is the recommend method for clearing the cache.
This is the most efficient method because tiles in the ambient cache are re-downloaded to remove outdated data from a device.
It does not erase resources from the ambient cache or delete the database, which can be computationally expensive operations that may carry unintended side effects. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | + + + + +```javascript +await MapboxGL.offlineManager.invalidateAmbientCache(); +``` + + #### resetDatabase() Deletes the existing database, which includes both the ambient cache and offline packs, then reinitializes it. diff --git a/docs/docs.json b/docs/docs.json index 428e3d2df..9f3b6fd71 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -5573,6 +5573,20 @@ } } }, + { + "name": "invalidateAmbientCache", + "description": "Forces a revalidation of the tiles in the ambient cache and downloads a fresh version of the tiles from the tile server.\nThis is the recommend method for clearing the cache.\nThis is the most efficient method because tiles in the ambient cache are re-downloaded to remove outdated data from a device.\nIt does not erase resources from the ambient cache or delete the database, which can be computationally expensive operations that may carry unintended side effects.", + "params": [], + "examples": [ + "await MapboxGL.offlineManager.invalidateAmbientCache();" + ], + "returns": { + "description": "", + "type": { + "name": "void" + } + } + }, { "name": "resetDatabase", "description": "Deletes the existing database, which includes both the ambient cache and offline packs, then reinitializes it.", diff --git a/ios/RCTMGL/MGLOfflineModule.m b/ios/RCTMGL/MGLOfflineModule.m index f29e633f4..37d6443b2 100644 --- a/ios/RCTMGL/MGLOfflineModule.m +++ b/ios/RCTMGL/MGLOfflineModule.m @@ -154,6 +154,17 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N }); } +RCT_EXPORT_METHOD(invalidateAmbientCache:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +{ + [[MGLOfflineStorage sharedOfflineStorage] invalidateAmbientCacheWithCompletionHandler:^(NSError *error) { + if (error != nil) { + reject(@"invalidateAmbientCache", error.description, error); + return; + } + resolve(nil); + }]; +} + RCT_EXPORT_METHOD(resetDatabase:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { [[MGLOfflineStorage sharedOfflineStorage] resetDatabaseWithCompletionHandler:^(NSError *error) { diff --git a/javascript/modules/offline/offlineManager.js b/javascript/modules/offline/offlineManager.js index 1d91d6fd6..bc8b502c2 100644 --- a/javascript/modules/offline/offlineManager.js +++ b/javascript/modules/offline/offlineManager.js @@ -90,6 +90,22 @@ class OfflineManager { } } + /** + * Forces a revalidation of the tiles in the ambient cache and downloads a fresh version of the tiles from the tile server. + * This is the recommend method for clearing the cache. + * This is the most efficient method because tiles in the ambient cache are re-downloaded to remove outdated data from a device. + * It does not erase resources from the ambient cache or delete the database, which can be computationally expensive operations that may carry unintended side effects. + * + * @example + * await MapboxGL.offlineManager.invalidateAmbientCache(); + * + * @return {void} + */ + async invalidateAmbientCache() { + await this._initialize(); + await MapboxGLOfflineManager.invalidateAmbientCache(); + } + /** * Deletes the existing database, which includes both the ambient cache and offline packs, then reinitializes it. *