Android MVP - How to create Model/Repository instance in background service to access model method?

Viewed 1122

I am using MVP pattern in My Android application. My requirement is

1. Sync captured data when user clicks 'Sync' button

2. Job-scheduler who will invoke the background service at midnight and sync data with server even though the app is not running.

The flow for both scenarios is:

1)Sync captured data when user clicks 'Sync' button

  • getPresenter().notifySyncBtnPressed() will be called

  • notifySyncBtnPressed() will instruct the Model by calling mModel.sync()

  • Sync from Model has entire implementation of fetching data from DB, upload it to server and notify result to Presenter
  • Presenter then notify to View by calling getView().notifySyncFinished()

Note: This flow is clear to me - suggest improvements if needed

2) Job-scheduler who will invoke the background service( Please provide your clarification here)

  • BroadcastReceiver will invoke Service class which extends IntentService
  • Create model object (IModel model = new Model()) and call model.sync() from onHandleIntent().
  • Since there is no UI, no need to invoke presenter. and no need to notify the status.
  • Service will get killed once sync operation is done.

Please suggest,

1. is it the right way to call Model (Repository) instance from Service class?

2. I am also confuse between Model layer and Repositiry layer, is there any difference in Model layer and Repository layer here in MVP.

1 Answers
Related