Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android MVVM/存储库如何强制LiveData从存储库更新?_Java_Android_Mvvm_Repository Pattern_Android Livedata - Fatal编程技术网

Java Android MVVM/存储库如何强制LiveData从存储库更新?

Java Android MVVM/存储库如何强制LiveData从存储库更新?,java,android,mvvm,repository-pattern,android-livedata,Java,Android,Mvvm,Repository Pattern,Android Livedata,我的问题是: 我使用的MVVM/存储库设计模式如下: 活动-(观察)->ViewModel的LiveData->Repository->WebService API(获取资源) 我还有另一个电话要求将资源更新到WebService 问题: 更改服务器上的资源后。如何使资源livedata使用新服务器数据自我更新 我想强制它再次从服务器获取数据,因为某些其他数据可能已更改。 我不想使用本地数据库(Room)并更改它,因为我的服务器数据可能会更改。他们每次都要去拿 我想到的唯一解决方案是为它创建一个

我的问题是:

我使用的MVVM/存储库设计模式如下:

活动-(观察)->ViewModel的LiveData->Repository->WebService API(获取资源)

我还有另一个电话要求资源更新到WebService

问题:

更改服务器上的资源后。如何使资源livedata使用新服务器数据自我更新

我想强制它再次从服务器获取数据,因为某些其他数据可能已更改。 我不想使用本地数据库(Room)并更改它,因为我的服务器数据可能会更改。他们每次都要去拿

我想到的唯一解决方案是为它创建一个Livedata源(作为dataVersion)。 并在每次更新后按如下方式递增(伪代码):


以及如何在ViewModel中更新数据版本。

您可以扩展
MutableLiveData
以提供手动获取功能

public class RefreshLiveData<T> extends MutableLiveData<T> {
    public interface RefreshAction<T> {
        private interface Callback<T> {
             void onDataLoaded(T t);
        }

        void loadData(Callback<T> callback);
    }

    private final RefreshAction<T> refreshAction;
    private final Callback<T> callback = new RefreshAction.Callback<T>() {
          @Override
          public void onDataLoaded(T t) {
               postValue(t);
          }
    };

    public RefreshLiveData(RefreshAction<T> refreshAction) {
        this.refreshAction = refreshAction;
    }

    public final void refresh() {
        refreshAction.loadData(callback);
    }
}
公共类RefreshLiveData扩展了可变LiveData{
公共接口刷新操作{
专用接口回调{
空载空隙率(T);
}
void loadData(回调);
}
私人最终行动;
private final Callback Callback=new RefreshAction.Callback(){
@凌驾
已加载的公共空间(T){
后置值(t);
}
};
公共RefreshLiveData(RefreshAction RefreshAction){
this.refreshAction=refreshAction;
}
公共最终无效刷新(){
refreshAction.loadData(回调);
}
}
那你就可以了

public class YourViewModel extends ViewModel {
    private RefreshLiveData<List<Project>> refreshLiveData;

    private final GithubRepository githubRepository;
    private final SavedStateHandle savedStateHandle;

    public YourViewModel(GithubRepository githubRepository, SavedStateHandle savedStateHandle) {
         this.githubRepository = githubRepository;
         this.savedStateHandle = savedStateHandle;

         refreshLiveData = Transformations.switchMap(savedStateHandle.getLiveData("userId", ""), (userId) -> {
             githubRepository.getProjectList(userId);
         });
    }

    public void refreshData() {
        refreshLiveData.refresh();
    }

    public LiveData<List<Project>> getProjects() {
        return refreshLiveData;
    }
}
公共类YourViewModel扩展了ViewModel{
私有RefreshLiveData RefreshLiveData;
私有最终GithubRepository GithubRepository;
私有最终SavedStateHandle SavedStateHandle;
公共YourViewModel(GithubRepository GithubRepository,SavedStateHandle SavedStateHandle){
this.githubRepository=githubRepository;
this.savedstatehold=savedstatehold;
refreshLiveData=Transformations.switchMap(savedStateHandle.getLiveData(“用户ID”),(用户ID)->{
githubRepository.getProjectList(userId);
});
}
公共数据(){
refreshLiveData.refresh();
}
公共LiveData getProjects(){
返回刷新数据;
}
}
然后,存储库可以执行以下操作:

public RefreshLiveData<List<Project>> getProjectList(String userId) {
    final RefreshLiveData<List<Project>> liveData = new RefreshLiveData<>((callback) -> {
         githubService.getProjectList(userId).enqueue(new Callback<List<Project>>() {
            @Override
            public void onResponse(Call<List<Project>> call, Response<List<Project>> response) {
                callback.onDataLoaded(response.body());
            }

            @Override
            public void onFailure(Call<List<Project>> call, Throwable t) {

            }
         });
    });

    return liveData;
}
public RefreshLiveData getProjectList(字符串用户ID){
final RefreshLiveData liveData=新的RefreshLiveData((回调)->{
getProjectList(userId).enqueue(新回调()命令){
@凌驾
公共void onResponse(调用、响应){
callback.onDataLoaded(response.body());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
});
返回liveData;
}

你为什么如此反对本地db?您必须做的唯一一件事就是在每次服务器的数据更改时更新它。据我所知,当数据发生变化时(云消息或其他信息),您希望并知道如何从服务器向应用程序发送信号。获取此消息后,您可以调用WebServiceAPI,更新本地数据库,然后您的ViewModel可以使用LiveData帮助轻松获取实际数据(当然是来自本地数据库的数据)。作为奖励,你的应用程序可以在没有互联网的情况下生存。我的用例是一个购物车,其中大多数商品都有折扣和折扣,并且在良好的生存公告中会有很多客户,因此每个用户的购物车实体数据都保存在服务器中的一个短时间缓存服务器中。应用程序购物车应该在每次重新加载时获取数据,以查看项目是否已从购物车中撤销。(顾客在购物车商品消失前只有5分钟的时间购买商品)。正因为如此,我不会将这些数据保存在本地数据库中,但我使用了缓存破坏机制并对每个更新的列表进行版本控制。
public RefreshLiveData<List<Project>> getProjectList(String userId) {
    final RefreshLiveData<List<Project>> liveData = new RefreshLiveData<>((callback) -> {
         githubService.getProjectList(userId).enqueue(new Callback<List<Project>>() {
            @Override
            public void onResponse(Call<List<Project>> call, Response<List<Project>> response) {
                callback.onDataLoaded(response.body());
            }

            @Override
            public void onFailure(Call<List<Project>> call, Throwable t) {

            }
         });
    });

    return liveData;
}