Skip to content

Persistence

Jóhannes Erlingsson edited this page May 15, 2017 · 1 revision

The Android SDK includes a way to easily integrate a user implemented persistence solution. To take advantage of the persistence features of the Meniga SDK a user of it must implement the interface Persistence Provider which has the following three methods:

<T> T fetch(QueryRequestObject key);
<T> void save(QueryRequestObject key, T value);
boolean hasKey(QueryRequestObject key);

Whenever the sdk makes a call, such as fetching a category, it routes the request through the users implementation of the PersistenceProvider interface. The first thing it does is query if the persistence provider has the key for the object that is being requested. The QueryRequestObject object acts as a key, by checking it's type and value, you can easily identify what is being queried.

If the persistence provider has a local copy of the requested object, it should return true from hasKey. This will cause the sdk to immediately call fetch instead of doing a network call. The persistence provider than has to retrieve the object using the QueryRequestObject to find out what exactly is being asked for (same as for hasKey).

If the object is not contained within the persistence store, a network call is made. When the network call returns the sdk will call save, providing the query object along with the retrieved object fresh from the back end.

Here's an example of how to persist categories. Categories are really good for caching since they do not change much:

public class CategoryCaching implements PersistenceProvider {
    List<MenigaCategory> cachedCategories;
    Map<Long, MenigaCategory> catMap = new HashMap<>();

    @Override
    public <T> T fetch(QueryRequestObject key) {
        if(key instanceof GetCategories) {
            return (T)this.cachedCategories;
        }
        else if(key instanceof GetCategoryById) {
            return (T)this.catMap.get(((GetCategoryById)key).categoryId);
        }
        return null;
    }

    @Override
    public void save(QueryRequestObject key, Object value) {
        if(key instanceof GetCategories) {
            this.cachedCategories = (List<MenigaCategory>) value;
        }
        else if(key instanceof GetCategoryById) {
            this.catMap.put(((GetCategoryById)key).categoryId, (MenigaCategory)value);
        }
    }

    @Override
    public boolean hasKey(QueryRequestObject key) {
        if((key instanceof GetCategories) && this.cachedCategories != null) {
            return true;
        }
        else if((key instanceof GetCategoryById) && this.catMap.containsKey(((GetCategoryById)key).categoryId)) {
            return true;
        }
        return false;
    }
}

Don't forget to initialize your SDK with your persistance provider.

One thing to take note of is that the SDK will not always query the persistance provider if it has a certain object. For many operations this does not make sense, such as when creating a new resource. In that case only the save method will be called.

Similarly some operations bypass the persistance system all together, such as operations that start a process, such as the sync procedure.

Clone this wiki locally