AsyncStorageModule extends ReactContextBaseJavaModule implements ModuleDataCleaner.Cleanable { @ReactMethod public void multiRemove(final ReadableArray keys, final Callback callback) { if (keys.size() == 0) { callback.invoke(AsyncStorageErrorUtil.getInvalidKeyError(null)); return; } new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) { @Override protected void doInBackgroundGuarded(Void... params) { if (!ensureDatabase()) { callback.invoke(AsyncStorageErrorUtil.getDBError(null)); return; } WritableMap error = null; try { mReactDatabaseSupplier.get().beginTransaction(); for (int keyStart = 0; keyStart < keys.size(); keyStart += MAX_SQL_KEYS) { int keyCount = Math.min(keys.size() - keyStart, MAX_SQL_KEYS); mReactDatabaseSupplier.get().delete( TABLE_CATALYST, AsyncLocalStorageUtil.buildKeySelection(keyCount), AsyncLocalStorageUtil.buildKeySelectionArgs(keys, keyStart, keyCount)); } mReactDatabaseSupplier.get().setTransactionSuccessful(); } catch (Exception e) { FLog.w(ReactConstants.TAG, e.getMessage(), e); error = AsyncStorageErrorUtil.getError(null, e.getMessage()); } finally { try { mReactDatabaseSupplier.get().endTransaction(); } catch (Exception e) { FLog.w(ReactConstants.TAG, e.getMessage(), e); if (error == null) { error = AsyncStorageErrorUtil.getError(null, e.getMessage()); } } } if (error != null) { callback.invoke(error); } else { callback.invoke(); } } }.execute(); } AsyncStorageModule(ReactApplicationContext reactContext); @Override String getName(); @Override void initialize(); @Override void onCatalystInstanceDestroy(); @Override void clearSensitiveData(); @ReactMethod void multiGet(final ReadableArray keys, final Callback callback); @ReactMethod void multiSet(final ReadableArray keyValueArray, final Callback callback); @ReactMethod void multiRemove(final ReadableArray keys, final Callback callback); @ReactMethod void multiMerge(final ReadableArray keyValueArray, final Callback callback); @ReactMethod void clear(final Callback callback); @ReactMethod void getAllKeys(final Callback callback);  }
@Test public void testMultiRemove() { final String key1 = "foo1"; final String key2 = "foo2"; final String value1 = "bar1"; final String value2 = "bar2"; JavaOnlyArray keyValues = new JavaOnlyArray(); keyValues.pushArray(getArray(key1, value1)); keyValues.pushArray(getArray(key2, value2)); mStorage.multiSet(keyValues, mock(Callback.class)); JavaOnlyArray keys = new JavaOnlyArray(); keys.pushString(key1); keys.pushString(key2); Callback getCallback = mock(Callback.class); mStorage.multiRemove(keys, getCallback); Mockito.verify(getCallback, Mockito.times(1)).invoke(); Callback getAllCallback = mock(Callback.class); mStorage.getAllKeys(getAllCallback); Mockito.verify(getAllCallback, Mockito.times(1)).invoke(null, mEmptyArray); mStorage.multiSet(keyValues, mock(Callback.class)); keys.pushString("fakeKey"); Callback getCallback2 = mock(Callback.class); mStorage.multiRemove(keys, getCallback2); Mockito.verify(getCallback2, Mockito.times(1)).invoke(); Callback getAllCallback2 = mock(Callback.class); mStorage.getAllKeys(getAllCallback2); Mockito.verify(getAllCallback2, Mockito.times(1)).invoke(null, mEmptyArray); }