Skip to content

[Android] Make db size configurable #99

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

Merged
merged 2 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,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).

## Writing tests

Expand Down
15 changes: 14 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['RNAsyncStorage_' + name]).toInteger()
}

// AsyncStorage has default size of 6MB.
// This 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.
// If you really need bigger size, please keep in mind the potential consequences.
long dbSizeInMB = 6L

def newDbSize = rootProject.properties['AsyncStorage_db_size_in_MB']

if( newDbSize != null && newDbSize.isLong()) {
dbSizeInMB = newDbSize.toLong()
}

apply plugin: 'com.android.library'

android {
Expand All @@ -27,6 +39,8 @@ android {
defaultConfig {
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')

buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
}
}

Expand All @@ -37,4 +51,3 @@ repositories {
dependencies {
implementation 'com.facebook.react:react-native:+'
}

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 @@ -43,7 +43,7 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper {

private Context mContext;
private @Nullable SQLiteDatabase mDb;
private long mMaximumDatabaseSize = 6L * 1024L * 1024L; // 6 MB in bytes
private long mMaximumDatabaseSize = BuildConfig.AsyncStorage_db_size * 1024L * 1024L;

private ReactDatabaseSupplier(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions docs/advanced/IncreaseDbSize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Increase Async Storage size

## Android

Current Async Storage's size is set to 6MB. Going over this limit causes `database or disk is full` error. 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

Add a `AsyncStorage_db_size_in_MB` property to your `android/gradle.properties`:

```
AsyncStorage_db_size_in_MB=10
```

Now you can define the new size in MB. In this example, the new limit is 10 MB.


## iOS

Async Storage size is not limited programmatically on iOS.
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# This is an example of how you can change default DB size (6MB) to 10MB
# AsyncStorage_db_size_in_MB=10