Skip to content

Commit 608e2d8

Browse files
alexkuttigtido64
authored andcommitted
feat: Make db size configurable on Android (#99)
1 parent bfa35cc commit 608e2d8

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ getData = async () => {
5858
}
5959

6060
```
61-
62-
See docs for [api and more examples](docs/API.md), and [brownfield integration guide](docs/AdvancedUsage.md).
61+
### Advanced
62+
See docs for [api and more examples](docs/API.md) or [advanced usages](docs/advanced).
6363

6464
## Writing tests
6565

android/build.gradle

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def getExtOrIntegerDefault(name) {
1818
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['RNAsyncStorage_' + name]).toInteger()
1919
}
2020

21+
// AsyncStorage has default size of 6MB.
22+
// This is a sane limit to protect the user from the app storing too much data in the database.
23+
// This also protects the database from filling up the disk cache and becoming malformed.
24+
// If you really need bigger size, please keep in mind the potential consequences.
25+
long dbSizeInMB = 6L
26+
27+
def newDbSize = rootProject.properties['AsyncStorage_db_size_in_MB']
28+
29+
if( newDbSize != null && newDbSize.isLong()) {
30+
dbSizeInMB = newDbSize.toLong()
31+
}
32+
2133
apply plugin: 'com.android.library'
2234

2335
android {
@@ -27,6 +39,8 @@ android {
2739
defaultConfig {
2840
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
2941
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
42+
43+
buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
3044
}
3145
}
3246

@@ -37,4 +51,3 @@ repositories {
3751
dependencies {
3852
implementation 'com.facebook.react:react-native:+'
3953
}
40-

android/src/main/java/com/reactnativecommunity/asyncstorage/ReactDatabaseSupplier.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
7+
88
package com.reactnativecommunity.asyncstorage;
99

1010
import javax.annotation.Nullable;
@@ -43,7 +43,7 @@ public class ReactDatabaseSupplier extends SQLiteOpenHelper {
4343

4444
private Context mContext;
4545
private @Nullable SQLiteDatabase mDb;
46-
private long mMaximumDatabaseSize = 6L * 1024L * 1024L; // 6 MB in bytes
46+
private long mMaximumDatabaseSize = BuildConfig.AsyncStorage_db_size * 1024L * 1024L;
4747

4848
private ReactDatabaseSupplier(Context context) {
4949
super(context, DATABASE_NAME, null, DATABASE_VERSION);
File renamed without changes.

docs/advanced/IncreaseDbSize.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Increase Async Storage size
2+
3+
## Android
4+
5+
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).
6+
7+
### Increase limit
8+
9+
Add a `AsyncStorage_db_size_in_MB` property to your `android/gradle.properties`:
10+
11+
```
12+
AsyncStorage_db_size_in_MB=10
13+
```
14+
15+
Now you can define the new size in MB. In this example, the new limit is 10 MB.
16+
17+
18+
## iOS
19+
20+
Async Storage size is not limited programmatically on iOS.

example/android/gradle.properties

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
# This option should only be used with decoupled projects. More details, visit
1717
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1818
# org.gradle.parallel=true
19+
20+
# This is an example of how you can change default DB size (6MB) to 10MB
21+
# AsyncStorage_db_size_in_MB=10

0 commit comments

Comments
 (0)