3

Im using MVVM Pattern and my Repository uses LiveData. But I need to access the data from BroadcastReceiver or IntentService. LiveData doesn't work in BroadcastReceiver because of missing LiveCircle and I read it isn’t recommended in Services. Do I need to change my Repository to ReactiveX or what's the best practise in this case?

Repository:

object MainRepository : Repository {

    override fun getGeofences(context: Context): LiveData<List<GeofenceModel>>? {
        return GeofenceDatabase.getInstance(context.applicationContext)?.geofenceDao()?.getGeofences
    }

    override fun createGeofence(context: Context, geofenceModel: GeofenceModel) {
        GeofenceDatabase.getInstance(context.applicationContext)?.geofenceDao()?.createGeofence(geofenceModel)
    }
}
Fintasys
  • 1,309
  • 1
  • 9
  • 16
  • You can use [LifecycleService](https://developer.android.com/reference/android/arch/lifecycle/LifecycleService) if you want a `Lifecycle` in a service. – ianhanniballake Oct 24 '18 at 02:50
  • 2
    Yea, I found that already here https://stackoverflow.com/questions/44708202/observe-livedata-from-foreground-service Is it really the only way? What about BroadcastReceiver? So I have my BroadcastReceiver to call a Service? – Fintasys Oct 24 '18 at 02:54
  • I want to communicate between activity and service and found that broadcasts are oldschool now, how would we communicate between activity and service, say I want to start service when something happens in activity and pass message to activity as well? – ateebahmed Mar 28 '19 at 15:34
  • @ateebahmed Guess what you looking for is a bound service. – Fintasys Apr 01 '19 at 08:30
  • nah sorry, actually I want the service to be running as well, I think I'll start service as foreground and bind it with activity to send messages, only thing remaining is sending messages from service to activity which others have suggested to use eventbus – ateebahmed Apr 02 '19 at 20:29
  • Mostly untested, but something like this might work maybe? https://ideone.com/pDXpxp I'm still working on it myself. – Mooing Duck Mar 29 '20 at 18:44

0 Answers0