我无法使用 mvvm 在两个存储库之间共享变量
I can't share variables between two repositories using mvvm
我需要在两个存储库之间共享两个变量。我已经尝试过许多不同的解决方案,但 none 奏效了。简而言之:第一个存储库包含用户最后已知的位置(地理纬度和经度),第二个存储库调用具有指定端点的 api,例如地理纬度和经度。问题是我不断收到那些等于 0,0 的值。你能给我一些提示我应该怎么做吗?我观察到的一件事是,整个程序立即运行,而位置存储库需要几秒钟才能实际获取纬度和经度。所以程序只是不断调用 api 端点等于 0,0 正如我上面提到的那样。
位置存储库
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 是如何工作的,你可以查看它 .
像这样更改您的 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;
}
}
Forcast 存储库
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 class,那么您也可以将您的位置数据作为列表或数组列表发送。只是我比较喜欢这个
已编辑:我已经更正了一些错误。现在代码正在运行。 Logcat 来自 ForcastRepository 中的 testCall() 方法 D/TAG: testCall: 90.3993212,23.7793183
我需要在两个存储库之间共享两个变量。我已经尝试过许多不同的解决方案,但 none 奏效了。简而言之:第一个存储库包含用户最后已知的位置(地理纬度和经度),第二个存储库调用具有指定端点的 api,例如地理纬度和经度。问题是我不断收到那些等于 0,0 的值。你能给我一些提示我应该怎么做吗?我观察到的一件事是,整个程序立即运行,而位置存储库需要几秒钟才能实际获取纬度和经度。所以程序只是不断调用 api 端点等于 0,0 正如我上面提到的那样。
位置存储库
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 是如何工作的,你可以查看它
像这样更改您的 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;
}
}
Forcast 存储库
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 class,那么您也可以将您的位置数据作为列表或数组列表发送。只是我比较喜欢这个
已编辑:我已经更正了一些错误。现在代码正在运行。 Logcat 来自 ForcastRepository 中的 testCall() 方法 D/TAG: testCall: 90.3993212,23.7793183