TutorialsCourses

Increase Android AsyncStorage Size in React Native

Increase Android AsyncStorage Size in React Native

The iOS AsyncStorage implementation has an unlimited amount of space by default. This is not the case on Android, the default size of AsyncStorage is 6MB. This is generally enough for a casual application however there are many cases when you may need more, like when you are using PouchDB async adapter.

To increase the size first locate the /android/app/src/main/java/MainApplication.java file in your React Native application.

We'll first add this import at the top of the MainApplication.java file

import com.facebook.react.modules.storage.ReactDatabaseSupplier;

Your imports may look something like.

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.facebook.react.modules.storage.ReactDatabaseSupplier;

Next locate the section that looks like

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }

We need to tell React Native how many bytes we want to allow as the maximum size.

We'll define the number of bytes we want like so

long size = 50L * 1024L * 1024L; // 50 MB

If you need more than 50MB change the 50L to your desired amount.

Finally we need to set the size with this line of code.

com.facebook.react.modules.storage.ReactDatabaseSupplier.getInstance(getApplicationContext()).setMaximumSize(size);

Our code after combining all changes will look like.

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    long size = 50L * 1024L * 1024L; // 50 MB
    com.facebook.react.modules.storage.ReactDatabaseSupplier.getInstance(getApplicationContext()).setMaximumSize(size);

  }

Disclaimer

This will work but is not recommended. If you need to store a significant amount of data look into using SQLite, PouchDB, Realm, or some other storage method. They will not only store data more efficiently but will also be queryable.