我有我的存储库和视图模型类,我在其中操作了改造 api 中的数据,这是正确的方法吗?

问题描述

我知道如何从微调器获取数据,然后将数据从 api 传递到存储库,但是在我的情况下,如何在 mvvm 中正确地从存储库获取数据到视图模型?在存储库中,我有 mutablelivedata,我想在视图模型中获取数据。我以自己的方式做到了这一点,但它是否正确有效?我只是想检查一下。

存储库类

public class DogsRepository {
public mutablelivedata<List<DogBreedModel>> mutableDogsBreedList = new mutablelivedata<>();
public mutablelivedata<Boolean> mutableDogLoadError = new mutablelivedata<>();
public mutablelivedata<Boolean> mutableLoading = new mutablelivedata<>();
private mutablelivedata<DogBreedModel> dogLiveData;
private DogsApiService dogsApiService;

public Single<List<DogBreedModel>> getApi() {
    dogsApiService = new DogsApiService();
    return dogsApiService.getDogs();
}

/* 这里我取了三个可变变量,应该是 Mutable 或 Live*/

查看模型类

public class Listviewmodel extends Androidviewmodel {
DogsRepository dogsRepository = new DogsRepository();
private Compositedisposable disposable = new Compositedisposable();
public Listviewmodel(@NonNull Application application) {
    super(application);
}
public mutablelivedata<List<DogBreedModel>> listmutablelivedata = dogsRepository.mutableDogsBreedList;
public mutablelivedata<Boolean> mutableLoading = dogsRepository.mutableLoading;
public mutablelivedata<Boolean> mutableDogLoadError = dogsRepository.mutableDogLoadError;

public void refresh() {
    fetchFromremote();
}
private void fetchFromremote() {
    mutableLoading.setValue(true);
    disposable.add(
            dogsRepository.getApi()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new disposableSingleObserver<List<DogBreedModel>>() {
                        @Override
                        public void onSuccess(List<DogBreedModel> dogBreedModels) {
                            listmutablelivedata.setValue(dogBreedModels);
                            mutableDogLoadError.setValue(false);
                            mutableLoading.setValue(false);
                        }
                        @Override
                        public void onError(Throwable e) {
                            mutableDogLoadError.setValue(true);
                            mutableLoading.setValue(false);
                            e.printstacktrace();
                        }
                    })
    );
}

@Override
protected void onCleared() {
    super.onCleared();
    disposable.clear();
}

然后通过可变类型方法将存储库可变数据带入视图模型。这是正确的方法吗?并通过这种方式保存获取api。

ApiService 类

public class DogsApiService {
private static final String BASE_URL = "https://raw.githubusercontent.com/";
private DogsApi api;

public DogsApiService() {
    api = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(DogsApi.class);
}

public Single<List<DogBreedModel>> getDogs(){
    return api.getDogs();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)