mercredi 17 août 2016

Using the Data Access Object pattern with Retrofit

I have an interface called, let's say, PlacesDataSource, for getting a list of places. It has a method called Place getPlaces().

I have two classes that implement this interface, one that gets places from a local database, one goes online and makes network requests.

For the local implementation, I have:

List<Place> getPlaces() {
    return mDatabase.getThoseGoodPlaces();
}

For the online implementation, I have:

List<Place> getPlaces() {
    Response<List<Place>> response = myOnlineApi.getThoseGoodPlaces().execute(); // synchronous request
    return response.body()
}

However, this would cause NetworkOnMainThreadException. I can change the interface method to void getPlaces() to use an asynchronous request. But this does not look right.

How does one usually approach this problem when using the Data Access Object pattern?

Aucun commentaire:

Enregistrer un commentaire