Skip to main content

Next storage implementation

Supported platforms:

Android


Motivation

Current implementation of persistence layer is created using SQLiteOpenHelper, a helper class that manages database creation and migrations. Even if this approach is powerful, the lack of compile time query verification and a big boilerplate of mapping SQLite queries to actual values make this implementation prone to many errors.

This Async Storage feature improves the persistence layer, using modern approaches to access SQLite (using Room), to reduce possible anomalies to the minimum. On top of that, it allows accessing AsyncStorage from the native side, useful in Brownfield integration.

Migration

This feature requires no migration from the developer perspective - the current database will be recreated (based on the current one), meaning user won't lose any data if you decide to opt in. There's a small drawback to know - the database "recreation" happens only once.

How it works

The new database (the one used by this feature) will be created based on the current database file, if the new one does not exist yet. If we detect that there's already the new database on the device, recreation will not kick in.

Why is it important

Let's say you enabled the feature for the first time - recreation kicks in and the old database file is untouched. If you decide to disable the feature, your users will be back using old database. No data migrations is happening from new to old database file. When you enable the feature again, the new database is not recreated, because it already exists, and no data is copied over.

Enable

Add config flag

In your project's android directory, locate gradle.properties file (if it does not exist, create one) and add the line:

AsyncStorage_useNextStorage=true

React Native < 0.73

For React Native below version 0.73, you need to apply Kotlin plugin to your project. In your project's android directory, locate root build.gradle file. Add Kotlin dependency to the buildscript:

 buildscript {
ext {
// other extensions
+ kotlinVersion = '1.9.24'
}

dependencies {
// other dependencies
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

Configuration

Kotlin version

Next storage is tested against Kotlin version 1.9.24. You can specify different version, in one of two ways:

  • add kotlinVersion extension to the rootProject:
rootProject.ext.kotlinVersion = '1.9.24'
  • specify AsyncStorage_kotlinVersion in gradle.properties:
AsyncStorage_kotlinVersion=1.9.24

Room

Next AsyncStorage uses Room persistence library to store data. Currently, tested version is 2.6.1. You can specify different version, by adding a flag to gradle.properties:

AsyncStorage_next_roomVersion=2.6.1

KSP is enabled for symbol processing for the Room library. KSP version will be selected based on Kotlin version in your project. If you want to use different KSP version, you can set a property in gradle.properties:

AsyncStorage_next_kspVersion=1.9.24-1.0.20

Notable changes

Alongside of a warning regarding key/value, errors are thrown when:

  • Your key is null or not a string
  • You provide value that is not a string