ObjectBox – the high performance Android database for Java / Kotlin – goes 3.0 ❤️ and apart from features. the dev team is also sharing CRUD performance benchmarks including MongoDB Realm and SQLite with Room. ObjectBox Database has been used by over 800,000 developers since the 1.0 release for Android and apart from Java / Kotlin for Android, the ObjectBox DB also has C/C++, Go, Flutter/Dart, Swift bindings now and a superfast Data Sync.
What is ObjectBox?
ObjectBox is a NoSQL ACID-compliant object database and an alternative to SQLite and Room. ObjectBox is optimized for fast object persistence on restricted devices, typically “embedded devices”, sometimes called “edge devices” like e.g. smartphones, IoT gateways, PoS systems, or Controlling Units. Because most applications today include any number of decentralized connected devices, ObjectBox also provides fast and easy access to decentralized edge data through an out-of-the-box Data Sync solution (Early Access).
New Java / Kotlin Data Query API
Similar to the ObjectBox Dart/Flutter Query API, the new Java / Kotlin Query API helps to create nested conditions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Kotlin // equal AND (less OR oneOf) val query = box.query( User_.firstName equal "Joe" and (User_.age less 12 or (User_.stamp oneOf longArrayOf(1012)))) .order(User_.age) .build() // Java // equal AND (less OR oneOf) Query<User> query = box.query( User_.firstName.equal("Joe") .and(User_.age.less(12) .or(User_.stamp.oneOf(new long[]{1012})))) .order(User_.age) .build(); |
In Kotlin, the condition methods are also available as infix functions. This can make queries easier to read:
1 |
val query = box.query(User_.firstName equal "Joe").build() |
Unique on conflict replace strategy
One unique property in an @Entity can now be configured to replace the object in case of a conflict — onConflict — when putting a new object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Kotlin @Entity data class Example( @Id var id: Long = 0, @Unique(onConflict = ConflictStrategy.REPLACE) var uniqueKey: String? = null ) // Java @Entity public class Example { @Id public long id; @Unique(onConflict = ConflictStrategy.REPLACE) String uniqueKey; } |
This can help when existing data is updated with a unique ID different from the ObjectBox ID. E.g. assume an app for downloading a set of playlists where each has a modifiable title (e.g. “My Playlist”) and a unique String ID (“jam-1”). If the title of “jam-1” has changed to e.g. “Old Playlist” when downloading an updated version of the playlists, just a single put with the new data will now be done. The existing object for “jam-1” is then deleted and replaced by the new version.
Built-in string array and map support
Two new property types are now supported out-of-the-box: string array and string map. The containsElement condition allows looking for objects by a specific item contained in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Kotlin @Entity data class Example( @Id var id: Long = 0, var stringArray: Array<String>? = null, var stringMap: MutableMap<String, String>? = null ) // matches [“first”, “second”, “third”] box.query(Example_.stringArray.containsElement(“second”)).build() // Java @Entity public class Example { @Id public long id; public String[] stringArray; public Map<String, String> stringMap; } // matches [“first”, “second”, “third”] box.query(Example_.stringArray.containsElement(“second”)).build(); |
Kotlin Flow, Android 12 and more
Kotlin extension functions were added to obtain a Flow from a BoxStore or Query:
1 |
val flow = box.query().subscribe().toFlow() |
For details on all changes, please check the ObjectBox for Java changelog.
Room (SQLite), Realm & ObjectBox CRUD performance benchmarks
The Android database CRUD benchmark includes ObjectBox, SQLite 3.22.0 used with Room 2.3.0 and MongoDB Realm 10.6.1. ObjectBox outperforms MongoDB Realm and SQLite with Room across all four major database operations: Create, Read, Update, Delete.
The benchmarks were run on a Samsung Galaxy S9+ (Exynos) mobile phone with Android 10. All tests were repeated 10+ times and no outliers were discovered. The results graph above depicts the averages. The benchmarking code is open source; check it out yourself.
A fast local database is more than just a “nice-to-have.” It saves device resources, so the resource-heavy operations are left with more CPU, Memory, and battery power. In addition, an edge database improves privacy and data ownership by design. This is because it allows you to keep more data locally with the device and user. It also results in reduced data transferral volumes that have a significant impact on sustainability.
Sustainable Data Sync
However, you might need to synchronize some data to a backend. Selective and efficient data synchronization, as well as reduced overhead, help reduce bandwidth strain and cloud / Mobile Network usage. This in effect lowers the CO2 emissions. If you are looking for an out-of-the-box solution, ObjectBox Data Sync is right for you.
Get started with ObjectBox for Java / Kotlin
If you want to get started with using the free ObjectBox Android database right away, check this getting-started article or this video tutorial. Or go directly to GitHub. Already an ObjectBox Android database user and ready to take your application to the next level? Check out ObjectBox Data Sync, which solves data synchronization for edge devices, out-of-the-box. It’s really very efficient and (you guessed it) superfast 😎