观察变量的变化

我有一个在多个地方(一个用户)使用的变量。我不想每次打开片段或活动时获得该信息,而减少网络流量。

我正在考虑的是一次获取此数据,然后在其他地方进行观察。

我了解有诸如Observable和Eventbuses之类的东西,我研究了RxJava2,但似乎找不到找到更新Observable所持有价值的方法。

你能告诉我我应该怎么做吗?我还想看看是否有一种方法可以每隔一段时间(例如,每1秒钟)重新更新一次数据。


牧羊人nacy
浏览 176回答 2
2回答

慕尼黑的夜晚无繁华

您可以按以下方式处理:您可以在一个Repository类中从网络中获取数据,并使该类成为单例。然后从服务中检索数据并将其发送到LiveData对象。现在,您可以观察到该LiveData对象以及从服务中分别检索数据的方法。因此,一旦检索到数据,便可以将其用于将来使用;如果要再次从服务中检索数据,则可以调用检索方法并进行观察。类似于以下代码:(注意:此代码仅是示例,由于我不了解您的模型,因此没有必要进行编译。)&nbsp;DataRepository {&nbsp; &nbsp; private static DataRepository instance;&nbsp; &nbsp; private final LiveData<User> userLiveData;&nbsp; &nbsp; private DataRepository() {}&nbsp; &nbsp; public synchronized static DataRepository getInstance(){&nbsp; &nbsp; &nbsp; &nbsp; synchronized (DataRepository.class) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(instance == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;instance = new DataRepository();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;instance.userLiveData = new MutableLiveData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return instance;&nbsp; &nbsp; }&nbsp; &nbsp; public LiveData<User> getUser(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return userLiveData;&nbsp; &nbsp; }&nbsp; &nbsp; public LiveData<User> fetchUser(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;callGetUserServiceAsync();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return userLiveData;&nbsp; &nbsp; }&nbsp; &nbsp; public void callGetUserServiceAsync(){&nbsp; &nbsp; &nbsp; &nbsp; //call your service asynchronously and when service call back is called do as below, this call sample is in retrofit format, yours might be different&nbsp; &nbsp; &nbsp; &nbsp; callService().enqueue( new CallBack<User>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onResponse(Call<IndraConfigure> call, Response<IndraConfigure> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userLiveData.postValue(response.body); //assuming that response.body is of type User&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Call<IndraConfigure> call, Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do something relevant when call fails to fetch user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;}然后,当您想从服务中检索用户时,在代码中(最好是使用MVVM的ViewModel),请执行以下操作:&nbsp;DataRepository.getInstance().fetchUser().observe() {&nbsp; ....&nbsp;}当您只想使用以前获取的数据时,请执行以下操作:DataRepository.getInstance().getUser().observe() {&nbsp;....}请注意,您必须fetchUser()至少调用一次该方法,然后才能通过调用获得用户数据getUser()。

料青山看我应如是

我认为您应该考虑将其LiveData与MVVM体系结构一起使用。TL; 博士简而言之,您将要做的是这样的事情:SharedSessionViewModelpublic class SessionSharedViewModel extends ViewModel {&nbsp; &nbsp; private LiveData<Session> sharedSession = new MutableLiveData()&nbsp; &nbsp; public SessionSharedViewModel() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; }&nbsp; &nbsp; public void setSession(Session data) {&nbsp; &nbsp; &nbsp; &nbsp; MutableLiveData<Session> casted = (MutableLiveData<Session>) this.sharedSession&nbsp; &nbsp; &nbsp; &nbsp; casted.postValue(data)&nbsp; &nbsp; }&nbsp; &nbsp; public LiveData<Session> getSharedSession() {&nbsp; &nbsp; &nbsp; &nbsp; return this.sharedSession;&nbsp; &nbsp; }}第一片段public class FirstFragment extends Fragment() {&nbsp; &nbsp; private SessionSharedViewModel session;&nbsp; &nbsp; // some sort of callback called when the user session arrives from the API&nbsp; &nbsp; public void methodThatHandlesUserData(Session user){&nbsp; &nbsp; &nbsp; &nbsp; session.getSharedSession().setSession(user);&nbsp; &nbsp; }&nbsp; &nbsp; public void onActivityCreated(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onActivityCreated(savedInstanceState)&nbsp; &nbsp; &nbsp; &nbsp; session = ViewModelProviders.of(this.activity).get(SessionSharedViewModel.class)&nbsp; &nbsp; &nbsp; &nbsp; session.sharedSession.observe({yourobservablehere});&nbsp; &nbsp; &nbsp; &nbsp; Observer<Session> userObserver = new Observer<>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onChanged(Session user){observableCallback(user);}&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; sharedViewModel.sharedSession.observe(userObserver);&nbsp; &nbsp; }&nbsp; &nbsp; public void observableCallback(newSession: Session) {&nbsp; &nbsp; &nbsp; &nbsp; // Do something when returning to this fragment&nbsp; &nbsp; }}详细片段class SessionDetailsFragment : Fragment() {&nbsp; &nbsp; private SessionSharedViewModel sharedViewModel;&nbsp; &nbsp; public void onActivityCreated(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onActivityCreated(savedInstanceState)&nbsp; &nbsp; &nbsp; &nbsp; sharedViewModel = ViewModelProviders.of(this.activity).get(SessionSharedViewModel.class)&nbsp; &nbsp; &nbsp; &nbsp; Observer<Session> userObserver = new Observer<>(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onChanged(Session user){observableCallback(user);}&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; sharedViewModel.sharedSession.observe(userObserver);&nbsp; &nbsp; }&nbsp; &nbsp; public void observableCallback(newSession: Session) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(activity, "This is inside new activity: "+newSession.getUsername(), Toast.LENGTH_SHORT);&nbsp; &nbsp; }}进一步阅读LiveData已被Android团队采用,它由的新androidx库完全支持,构成的一部分Android Arquitechture Components。使用LiveData提供了一组不错的良好功能,例如:没有内存泄漏您的应用的用户界面与数据状态相符LiveData知道生命周期,因此不会再因Activity生命周期(ViewModel<3&nbsp;LiveData)而崩溃在活动和片段之间共享资源的能力为了进一步阅读,我建议您去这里
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java