Skip to content

Instantly share code, notes, and snippets.

@barbeau
Last active May 24, 2021 19:06
Show Gist options
  • Save barbeau/bbfe3280b59462bae94bc1ddb218bc68 to your computer and use it in GitHub Desktop.
Save barbeau/bbfe3280b59462bae94bc1ddb218bc68 to your computer and use it in GitHub Desktop.
Medium article - Room + Flow for location updates
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
...
// Repository (in this case, Room database) from which to receive location updates via Flow, injected via Hilt
@Inject
lateinit var repository: LocationRepository
override fun onCreate(savedInstanceState: Bundle?) {
...
// Observe locations via Flow as they are inserted into Room by the Service
repository.getLocations()
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.onEach {
if (it.isNotEmpty()) {
logResultsToScreen("Foreground location: ${it[it.size - 1].toText()}")
}
}
.launchIn(lifecycleScope)
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment