Skip to content

Where Content Provider and Service would fit? #47

Open
@BillBosiolis

Description

@BillBosiolis

This is not an issue, just an architectural question...

Let's say that our application has a SQLite database, a Content Provider that exposes this data and an IntentService that is responsible for syncing data by calling the REST service and updating the database.

Where these two components should be placed?

I think The ContentProvider would easily be placed in the data module.
An additional Repository implementation in the data module would query the content provider and transform the cursor data into entity objects in order to be consumed by the Use Cases. For the UseCase there's no difference. It will still talk to the interface and it will get back the data it wants. Of course, the proper concrete implementation would be injected via dagger.

The Service could also be placed in the data module.

A new interface could be introduced in the domain, i.e. SyncRepository which will be used by the Use Case. The actual implementation of it (in the data module) will start the service with an Intent.

What do you think?

Activity

J-vandenBerg

J-vandenBerg commented on Oct 20, 2015

@J-vandenBerg

+1

cesards

cesards commented on Oct 21, 2015

@cesards

That's actually a very good question.

alexandru-calinoiu

alexandru-calinoiu commented on Oct 21, 2015

@alexandru-calinoiu

The content provider is just another data source for the repository so I agree with your decision.

I will place the service on the presentation layer, is a services folder, it will trigger a use case to sync the data using just a repository implementation. I think that the intent service is just another way of inputing data in the system.

BillBosiolis

BillBosiolis commented on Oct 21, 2015

@BillBosiolis
Author

One of the things that concerns me is that these components cannot run in sequence.

In the current implementation, when the UI asks for data the following take place

  • the Presenter invokes the UseCase
  • the UseCase will ask the Repository
  • the Repository implementation will hit the server if needed (if there is no cached data) and will return the results back to caller

In case a Service is used, the workflow could be the following

  • the Presenter invokes the UseCase
  • the UseCase will ask the Repository
  • the Repository implementation will return the data querying the local database. In addition, it will start the IntentService to fetch new data from the REST service
  • the REST data are saved onto the database

After that the UI should be informed about the changes and update its contents.

We can accomplish this by using a ContentObserver but in that case, the presentation layer is bound to a concrete data implementation (ContentProvider), so I don't think this is a good idea.

Another option, is that the data layer keeps a reference to the Subscriber (UseCase) and when needed (new changes saved onto the db) informs him again. The UseCase in turn will notify the Presentation's Subscriber which will ask from the View to update.

@alexandru-calinoiu I agree that the Service could be placed in the presentation layer. For instance, the user may need to manually refresh the data, so the Presenter can start the Service.

alexandru-calinoiu

alexandru-calinoiu commented on Oct 22, 2015

@alexandru-calinoiu

I will comment on this part:

the Repository implementation will return the data querying the local database. In addition, it will start the IntentService to fetch new data from the REST service

I don't understand why is a IntentService needed?

The way I've done this in the past is using a reactive, with rxjava, have a Repository implemetation that return Obeservable and subscribe to this, I have an example of this here

BillBosiolis

BillBosiolis commented on Oct 23, 2015

@BillBosiolis
Author

@alexandru-calinoiu I haven't looked at your example in great detail but I would use an IntentService along with a SyncAdapter if I wanted to sync my data regularly without necessarily the need to display the "new" data to the UI at that moment.

sinistance

sinistance commented on Nov 20, 2015

@sinistance

good explaination @BillBosiolis. i think this repo should have a branch that represents how to deal with databases because many of us is using database in everyday project.

connexion2000

connexion2000 commented on Feb 17, 2016

@connexion2000

And what about CursorLoaders, where do they belong?
How should I pass Cursor from data layer (where content provider belongs) to presentation layer (where cursor adapter belongs)?

lenguyenthanh

lenguyenthanh commented on Feb 18, 2016

@lenguyenthanh

+1

jpventura

jpventura commented on Feb 18, 2016

@jpventura

@BillBosiolis, I try to follow the approach presented at Google I/O 2010 talk "Android REST client applications", which is the same approach used by Udacity Android Nanodegree course.

Basically you can use:

talhakosen

talhakosen commented on Mar 24, 2016

@talhakosen

+1

10 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sevar83@sinistance@lenguyenthanh@alexandru-calinoiu@joaocsousa

        Issue actions

          Where Content Provider and Service would fit? · Issue #47 · android10/Android-CleanArchitecture