I want to request a sync of my sync adapter every time a change happens to some of my data. Having LiveData I wonder if the correct way would be to bind a observer to the data within the application lifecycle.
Does anyone know what the "official" way of doing this is?
Related
So, I am using workmanager to retrieve data from an FTP server. I have to display one type or information or another type deppending on the state of the worker.
I was using getWorkInfoByIdLiveData on the viewmodel and I was observing the state of the worker on the activity. But, as the Observer is almost deprecated I am trying to use Flow instead.
The problem that I have is that I don't know why, I can't use the asStateFlow() on the livedata. enter image description here
Does anyone know a way to observe the state of a worker without using LiveData and Observer?
Thank youuu
I am using the Single-Activity and Multiple-Fragments approach, and I have one fragment that has a ViewModel with LiveData. The UI always changes when there is a change in a remote database (Firebase Realtime Database). I followed steps that are described in the offical Firebase Blog (https://firebase.googleblog.com/2017/12/using-android-architecture-components.html). However, the fragment, which the LiveData and ViewModel are bound to, is not active (because I navigated to another fragment) I don't get a notification if something changes in the database.
What I would actually want is to get informed when something changes in the Firebase database no matter which fragment is active, because I would like to play a small notification sound such that the user knows that something has changed. When the fragment that is bound to the LiveData is not active, the UI should not be updated, but the notification sound should be played.
My question is whether this is possible with LiveData (Mutable LiveData) in combindation with Firebase Realtime-Database and if so, how can I do this? I think by default, LiveData only notifies and executes code, if the fragment is active.
Reminder: Any idea how to do that? I'll appreciate every comment.
There are a few ways of doing that depending on the desired behavior. If you want to be notified whenever data changes and your application is active you can do the following:
Ensure that you have a data or repository class that is scoped to the lifecycle of the application. Since you are using single activity architecture, you can also use an activity scoped view model.
Firebase already provides you a way of being notified whenever data changes through onDataChange() listener. You should listen to these changes in you data/repo/viewmodel class and react to changes whenever your data changes.
If you have the scoping done correctly, you'll be notified regardless of which fragment you are on. You can then react to those changes (play sound) as needed.
Right now I am trying to create a process, where I want to insert data in table and some observers will get notified and able to edit the data upon their interest. Below is a rough idea on how to do it. Can anyone please suggest the model/arch how I can achieve this?
Follow this tutorial on Room, ViewModel, and LiveData.
Room is a SQL database abstraction which can expose its data through LiveData. ViewModel is a class that holds data for the UI and survives configuration changes. LiveData is an observable container for data that is aware of the android lifecycle so you don't have to manage it in the lifecycle callbacks.
Basically, you create a Room database then expose LiveData objects to the ViewModel. The ViewModel in turn exposes LiveData objects to the fragment/activity. The fragment or activity observes the ViewModel's LiveData by attaching an Observer. The Observer defines how the fragment/activity reacts to changes in the data.
If you prefer RXJava you can follow this tutorial instead. It's essentially the same, but instead of exposing data with LiveData you use reactive streams.
Edit: here's a really good article on architecture: https://proandroiddev.com/android-architecture-starring-kotlin-coroutines-jetpack-mvvm-room-paging-retrofit-and-dagger-7749b2bae5f7
I have a fragment and the data displayed are fetched from a network call.
I am using onSaveInstanceState in order to avoid fetching the data again when the orientation changes, but if I understand the lifecycle for fragments correctly as long as the app is never destroyed (either explicit by the user or because the Android OS kills the activity due to lack of resources) the data from the server will never be refreshed.
If I have understood this part correctly, I would need to define some way to periodically refetch the data from the server or is there another way?
There are no rules about data refresh. It depends on your app, your data, etc. If you fetch a list of receipts, you do not need to implement a refresh mechanism. But if you fetch, I don't know, the exchange rate of currencies, you must have one.
What about a pull to refresh pattern ?
I would use the Android Architecture components ViewModel and LiveData to save the data during screen rotations preventing multiple calls to the server for screen rotation changes. This is the preferred method Google seem to be pushing to their developers https://developer.android.com/topic/libraries/architecture/saving-states
To prevent the data from going stale I would either let the user decide when to update using the "Pull to Update" (as described in the other answer) or add a timer to update if the fragment has been in the foreground for an extended period of time using the method described here: https://guides.codepath.com/android/Repeating-Periodic-Tasks
I work with RxJava and RxAndroid. How resume the work of an observable if a configuration change occurs (activity rotation, language locale change etc.)?
I need not only resume the work of an observable, also I need save emitted items, when subscriber is unsubscribed and emit all saved items right away after subscription.
I read a ton of articles, but I didn't find the answer to my question.
I found a few examples, but none of them does not solve the problem:
https://github.com/alapshin/rxweather
https://github.com/kaushikgopal/RxJava-Android-Samples
https://github.com/tehmou/rx-android-architecture
https://github.com/richardradics/RxAndroidBootstrap
You can use one of the ConnectableObservables. Particularly, cache or replay would be handy for this kind of situation.
For example, you can call cache on your observable, unsubscribe it when activity is destroyed, and resubscribe it again after the activity is recreated.
I have made a demo application (https://github.com/pmellaaho/RxApp) in order to experiment how to tackle these kinds of situations. Basically, I use a singleton model from Activity to get the response from network. This makes it possible to cache responses, access the data from multiple UI components, subscribe to pending request and also to provide mock data for automated UI tests.
You can use OperatorFreeze from this library for pausing Observable when activity is recreating. You need pass instance of this operator to method Observable.lift(). You don't need to unsubscribe from Observables when you use persistent Presenter.