Skip to content

[Android] Make db size configurable #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ getData = async () => {

```

See docs for [api and more examples](docs/API.md), and [brownfield integration guide](docs/AdvancedUsage.md).
### Advanced
See docs for [api and more examples](docs/API.md) or [advanced usages](docs/advanced).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the link here still correct?


## Writing tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ synchronized void scheduleNext() {

private final SerialExecutor executor;

public AsyncStorageModule(ReactApplicationContext reactContext) {
this(reactContext, AsyncTask.THREAD_POOL_EXECUTOR);
public AsyncStorageModule(ReactApplicationContext reactContext, long size) {
this(reactContext, AsyncTask.THREAD_POOL_EXECUTOR, size);
}

@VisibleForTesting
AsyncStorageModule(ReactApplicationContext reactContext, Executor executor) {
AsyncStorageModule(ReactApplicationContext reactContext, Executor executor, long size) {
super(reactContext);
this.executor = new SerialExecutor(executor);
mReactDatabaseSupplier = ReactDatabaseSupplier.getInstance(reactContext);
mReactDatabaseSupplier = ReactDatabaseSupplier.getInstance(reactContext, size);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@
import java.util.List;

public class AsyncStoragePackage implements ReactPackage {

long mSize = 6L * 1024L * 1024L;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?


public AsyncStoragePackage(){
super();
}

public AsyncStoragePackage(long size){
super();
mSize = size;
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new AsyncStorageModule(reactContext));
return Arrays.<NativeModule>asList(new AsyncStorageModule(reactContext, mSize));
}

// Deprecated in RN 0.47
// Deprecated in RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
Expand All @@ -33,4 +45,4 @@ public List<Class<? extends JavaScriptModule>> createJSModules() {
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.reactnativecommunity.asyncstorage;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -45,14 +45,15 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper {
private @Nullable SQLiteDatabase mDb;
private long mMaximumDatabaseSize = 6L * 1024L * 1024L; // 6 MB in bytes

private ReactDatabaseSupplier(Context context) {
private ReactDatabaseSupplier(Context context, long size) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
mMaximumDatabaseSize = size;
}

public static ReactDatabaseSupplier getInstance(Context context) {
public static ReactDatabaseSupplier getInstance(Context context, long size) {
if (sReactDatabaseSupplierInstance == null) {
sReactDatabaseSupplierInstance = new ReactDatabaseSupplier(context.getApplicationContext());
sReactDatabaseSupplierInstance = new ReactDatabaseSupplier(context.getApplicationContext(), size);
}
return sReactDatabaseSupplierInstance;
}
Expand Down
File renamed without changes.
32 changes: 32 additions & 0 deletions docs/advanced/IncreaseDbSize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Increase Async Storage size

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put here sections for platforms, i.e use H2 for Android and below iOS (which is not supported for now).

## Android

Current Async Storage's size is set to 6MB. Going over this limit causes `database or disk is full` error.

```
database or disk is full (code 13)
```

Disclaimer: This 6MB limit is a sane limit to protect the user from the app storing too much data in the database. This also protects the database from filling up the disk cache and becoming malformed (endTransaction() calls will throw an exception, not rollback, and leave the db malformed). You have to be aware of that risk when increasing the database size. We recommend to ensure that your app does not write more data to AsyncStorage than space is left on disk. Since AsyncStorage is based on SQLite on Android you also have to be aware of the [SQLite limits](https://www.sqlite.org/limits.html).

### Increase limit

Modify your `MainApplication.java`'s `getPackages`:

```
new AsyncStorage()
```

to

```
new AsyncStorage(100L * 1024L * 1024L)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bad practice to pass parameters to getPackages. It will also prevent autolinking available in RN 0.60 to work with this package. Please move the initialization logic to JS, something like:

// helpers/Storage.js
import {createAsyncStorage} from '@react-native-community/async-storage';
const AsyncStorage = createAsyncStorage(100 * 1024 * 1024);
export default AsyncStorage;

```

Where the 100 defines the new size in MB. In this example, new limit is 100 MB.


## iOS

Async Storage size is not limited programmatically on iOS.