gpt4 book ai didi

java - 我无法使用mvvm在两个存储库之间共享变量

转载 作者:行者123 更新时间:2023-12-03 10:31:05 24 4
gpt4 key购买 nike

我需要在两个存储库之间共享两个变量。我已经尝试了许多不同的解决方案,但是没有一个可行。简而言之:第一个存储库包含用户的最后一个已知位置(地理纬度和经度),第二个存储库使用指定的端点(例如地理纬度和经度)调用api。事情是我同意接收那些等于0,0的值。您能给我些提示我应该怎么做吗?我观察到的一件事是,整个程序立即运行,而位置存储库需要几秒钟才能真正获得纬度和经度。因此,程序只是不断调用端点等于0,0的api,如上所述。
位置存储库

    public class LocationRepository {

private double mLatitude, mLongitude;

public void test(Application application) {
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(application.getApplicationContext());
{
if (ContextCompat.checkSelfPermission(
application.getApplicationContext(), Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(application.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(application.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

client.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
}
});
}
}
}
}
预测存储库
public MutableLiveData<ForecastModel> testCall() {
MutableLiveData<ForecastModel> data = new MutableLiveData<>();

mApi.test(mLatitude, mLongitude, "metric", API_KEY).enqueue(new Callback<ForecastModel>() {
@Override
public void onResponse(Call<ForecastModel> call, Response<ForecastModel> response) {
if (!response.isSuccessful()) {
Log.i(TAG, "onResponse: " + response.code());
}
data.setValue(response.body());
}
@Override
public void onFailure(Call<ForecastModel> call, Throwable t) {
Log.i(TAG, "onFailure: " + t.getMessage());
}
});
return data;
}
ViewModel
private ForecastRepository mForecastRepository;
private LocationRepository mLocationRepository;

private MutableLiveData<ForecastModel> mForecastData;

public ForecastViewModel(@NonNull Application application) {
super(application);
mLocationRepository = new LocationRepository();
mLocationRepository.test(application);

mForecastRepository = new ForecastRepository();
mForecastData = mForecastRepository.testCall();
}

最佳答案

您可以在ViewModel和Transformation.switchMap()中使用两个livadata来跟踪更改。每次mLocationData更改时,mForcastData也会更新。如果您不知道switchMap的工作方式,可以检查How and where to use Transformations.switchMap?
像这样更改您的ViewModel和LocationRepository。
ViewModel

public class ForecastViewModel extends AndroidViewModel {

private ForcastRepository mForecastRepository;
private LocationRepostiory mLocationRepository;

private LiveData<ForecastModel> mForecastData;
private LiveData<LocationModel> mLocationData;

public ForecastViewModel(@NonNull Application application) {
super(application);
mLocationRepository = new LocationRepostiory();
mLocationRepository.test(application);
mLocationData = mLocationRepository.getMutableLiveData();
mForecastRepository = new ForcastRepository();

mForecastData = Transformations.switchMap(mLocationData, location->
mForecastRepository.testCall(location.getLatitude(), location.getLongitude()));
}

public LiveData<ForecastModel> getmForecastData() {
return mForecastData;
}
}
位置信息库
class LocationRepostiory {

private LocationModel mLocation;
private MutableLiveData<LocationModel> mutableLiveData = new MutableLiveData();

public void test(Application application) {
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(application.getApplicationContext());
{
if (ContextCompat.checkSelfPermission(
application.getApplicationContext(), Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(application.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(application.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

client.getLastLocation().addOnSuccessListener(location -> {
mLocation = new LocationModel(location.getLongitude(),location.getLatitude());
mutableLiveData.setValue(mLocation);
});
}
}
}

public MutableLiveData<LocationModel> getMutableLiveData() {
return mutableLiveData;
}
}
预测存储库
public MutableLiveData<ForecastModel> testCall(double mLatitude, double mLongitude ) {
MutableLiveData<ForecastModel> data = new MutableLiveData<>();

mApi.test(mLatitude, mLongitude, "metric", API_KEY).enqueue(new Callback<ForecastModel>() {
@Override
public void onResponse(Call<ForecastModel> call, Response<ForecastModel> response) {
if (!response.isSuccessful()) {
Log.i(TAG, "onResponse: " + response.code());
}
data.setValue(response.body());
}
@Override
public void onFailure(Call<ForecastModel> call, Throwable t) {
Log.i(TAG, "onFailure: " + t.getMessage());
}
});
return data;
}
位置模型
class LocationModel {
private Double longitude, latitude;

public LocationModel(Double longitude, Double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}

public Double getLongitude() {
return longitude;
}

public Double getLatitude() {
return latitude;
}
}
如果您不想制作额外的LocationModel类,则也可以将位置数据作为List或Arraylist发送。我只喜欢这个。
编辑:有些错误我已纠正。现在代码正在工作。来自ForcastRepository D/TAG中的testCall()方法的Logcat:testCall:90.3993212,23.7793183

关于java - 我无法使用mvvm在两个存储库之间共享变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65579732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com