Aerogear Guides Tutorials to help get you off and running.

AeroGear Android Data Store

AeroGear Android provides a simple data storage API. This API is useful for caching responses, sharing data among different systems, or providing some form of limited offline support.

How to use it

Creating a Store

To create a Store, invoke DataManager config method, passing along a StoreConfiguration. MemoryStoreConfiguration for an in-memory backed store, or SQLStoreConfiguration for an sqlite backed store. A name should be also provided to distinguish between different stores that this DataManager can hold.

DataManager.config("myMemoryStoreName", MemoryStoreConfiguration.class)

Store store = dataManager.get("myMemoryStoreName");

Note: A Memory store will go away when the application is removed from memory and is best suited for short term caching.

DataManager.config("mySQLStoreName", SQLStoreConfiguration.class)

SQLStore store = (SQLStore) dataManager.get("mySQLStoreName");*callback*/);
// or

Note: for an SQLStore you should make sure that 'open' or 'openSync' have been called before accessing the store. Otherwise, the store will be opened when a request is made which may impact performance.

You also can do something after Store creation adding a listener in a config

.addOnStoreCreatedListener(new OnStoreCreatedListener() {
    public void onStoreCreated(StoreConfiguration<?> storeConfiguration, Store<?> store) {
        // Here you can do something with the store using the configurations

Object ID’s

All objects which are stored or read from a store need to have an ID field. This field is annotated with the @RecordId annotation in your class file.

private UUID id

The Store implementation will use an IdGenerator object to generate an id when you save the object. By default the Store will use DefaultIdGenerator to create a new UUID value for your id field. If you are not using a UUID to represent your id field, you need to implement your own IdGenerator

DataManager.config("myMemoryStoreName", MemoryStoreConfiguration.class)
           .withIdGenerator(new MyIdGenerator())

PS: AeroGear does not control the id value. This is delegated to IdGenerator, so it’s your (developer) responsibility to control it.

Saving Data

The method save is used to insert a new record or update an existing one. To distinquish between the two, the @RecordId annotation will be scanned on the model class and if the field with this annotation is found to be null, a new record will be inserted otherwise an update will be applied.

MyModel data = new MyModel();
data.setName("AeroGear Android");
data.setDescription("Awesome library");;

Clearing the data store


Reading All Data

Collection<MyModel> allData = store.readAll();

Deleting Data

store.remove(1); // Deletes the element with id 1

Take a look at the complete example in our cookbook app

Encrypted Stores

If your storage information needs to be encrypted, AeroGear provides convenient and transparent encrypted storage support, building on the security foundation provided by aerogear-crypto-java, encrypted variants of both memory (EncryptedMemoryStore) and SQLite (EncryptedSQLStore)

DataManager.config("myEncryptedSQLStoreName", EncryptedSQLStoreConfiguration.class)
           .usingPassphrase("My awesome passphrase")

If you want to see it all together, give a try to AeroGear Crypto Demo app. The application demonstrates how you can use encrypted storage to store in a central point all your passwords.