Android Storage : Object Box , Easy to use Android database

March 14, 2018 , 0 Comments

Recently they appeared with a new library called Objectbox. This helps manage the local data storage of your app. Their older library grindo also works for the same purpose, but object-box solutions for local storage solutions are their next step. The biggest difference is that the ObjectBox does not use all SQL.

Getting Started

To get started using it, you can check out their gradle setup. Once you have gradle setup, the library is easy to use. Simply create your data model classes as normal, and add the annotation @Entity. See an example below:
public class Animal {
@Id(assignable = true)
private long id;
private String name;
private boolean flying;
private boolean swimming;
private boolean walking;
public long getId() {
return id;
public void setId(long id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;

public boolean isFlying() {
return flying;

public void setFlying(boolean flying) {
this.flying = flying;

public boolean isSwimming() {
return swimming;

public void setSwimming(boolean swimming) {
this.swimming = swimming;

public boolean isWalking() {
return walking;

public void setWalking(boolean walking) {
this.walking = walking;

ObjectBox requires access to the parts of your class, so you need to make a precise setter and procurement methods. On the other hand, you can remove the custom modifier and then click on the ObjectBox classes instead.

Now, when you have the structure of your model data, you should start the database. I chose to do this in the application class, but if you want, you can build your own person or you can put it on the Dagger. My use class looks at it:
public class App extends Application {

private static App sApp;
private BoxStore mBoxStore;

public void onCreate() {
sApp = this;
mBoxStore = MyObjectBox.builder().androidContext(App.this).build();

public static App getApp() {
return sApp;

public BoxStore getBoxStore() {
return mBoxStore;


Now that I have my database setup, I can access it using the BoxStore class. Instead of the typical SQL tables, they have boxes. Here are some example usages of a box:
BoxStore boxStore = App.getApp().getBoxStore();
Box<Animal> animalBox = boxStore.boxFor(Animal.class);

// loads all animals
List<Animal> animals = animalBox.getAll();

// find a specific animal in the database
long myDogId = 12;
Animal myDog = animalBox.get(myDogId);

// insert an animal into the database

// update an animal

//query for all the flying animals
List<Animal> flyingAnimals = animalBox.query().equal(Animal_.isFlying, true).build().find();

//delete all flying animals from the database

These are simple examples of the basic CRUD operations. You can do more complex queries too, and without having to write confusing SQL statements. ObjectBox creates queries with a fluent API, so chaining conditions is easy and understandable. To learn more about writing queries, check their documentation.


Like most other databases, ObjectBox has support for relations. It has support for one-to-one, one-to-many relations and many-to-many relations. Here’s an example of what a one-to-many relation looks like
public class Zoo {

private long id;

// a Zoo can have many Animals
ToMany<Animal> animals;


public class Animal {
@Id(assignable = true)
private long id;

private String name;

private boolean flying;

private boolean swimming;

private boolean walking;

// an Animal belongs to one Zoo
ToOne<Zoo> zoo;

This defines a relation between a Zoo, which has many Animals, and the Animal, which belongs to one Zoo. Currently you must make relations at least package private for ObjectBox.

To modify these relations or access them, it looks like this:
Zoo myZoo = new Zoo();

Animal elephant = new Animal();
Animal giraffe = new Animal();

// To-one relation: Set the Zoo that an animal belongs to and save it to the database

// To-one relation: Get the Zoo that an animal belongs to
Zoo elephantZoo = elephant.zoo.getTarget();

// To-many relation: Add Animals to the Zoo and save it to the database

// To-many relation: Get Animals that belongs to a Zoo
List<Animal> zooAnimals = myZoo.animals;


Unlike other database libraries (including their own greenDAO), migrations are almost entirely automatic. So you don’t need to worry about managing each schema version.

For example, if you add a new property or delete a property, there’s no migration code required. There are only two cases you need to be concerned with in terms of migration. Renaming a property or class, and changing a property type. They include documenation on how to handle these cases here. In most cases though, there will be no work required for migrations.


Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google



Best Watch Store

Popular Posts

Contact Form


Email *

Message *