1
votes

I am fetching orders from a backend that has two endpoints, one for fetching a limited number of orders and one for fetching simply the number of total orders available. I can store the orders using a CustomerOrder entity class but how can I store the primitive count value?

Since ObjectBox will not allow boxes of primitive types and requires using entity classes I had to wrap a single (!) Integer inside an entity class. Maybe there is a simpler way to do this?

What does not work but what I basically want to do:

Box<Integer> countBox = boxStore.boxFor(Integer.class);

My current entity wrapper class (still only uses 1 row in the table):

@Entity
public class CustomerOrderCount {

    @Id
    public Integer count;
}

I simply want to follow best practices and simplify things. Maybe there is an even easier method than using ObjectBox that is still legit that I am not seeing here.

1
ObjectBox is used to map entity classes to boxes. Other databases have similar data structures (e.g. tables or documents). If you only want to store a key + value, for Android possibly look at SharedPreferences or for Java at Properties as a better alternative. - Uwe - ObjectBox
I agree but is SharedPreferences the way to go? Is it bad practice to store a single value in a table? I read that you should be careful about using SharedPrefs. - Pontus Drejer
It's not bad practice. I just suggested an easier way as asked for :) - Uwe - ObjectBox
@Uwe-ObjectBox Thank you! If you want, you can post this as an answer and I will accept it. :) - Pontus Drejer

1 Answers

1
votes

For this can go for SharedPreferences.

Or if you have too many primitive values to store you can go for LevelDB

From the docs:

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.

The android bindings for LevelDB are available here.

You can also use Hawk. It's another simple key value storage for Android.