У меня проблемы с асинхронным программированием в приложении для Android. У меня есть код в классе ClientLocation
, который получает широту/долготу пользователя, но как только я их получу, мне нужно передать их функции. Просматривая код, я вижу, что вызов repository.getDeals
выполняется до ввода замыкания gotLocation
.
Вот важный фрагмент кода:
...
this.repository = Repository.getInstance();
ClientLocation.LocationResult locationResult = new ClientLocation.LocationResult() {
@Override
public void gotLocation(Location location) {
userLatitude = Double.toString(location.getLatitude());
userLongitude = Double.toString(location.getLongitude());
}
};
ClientLocation clientLocation = new ClientLocation();
clientLocation.getLocation(getApplication().getApplicationContext(), locationResult);
// I need the latitude and longitude values at this point in order to properly call getDeals
// currently when this is called the userLat/lon values are 0.0 because gotLocation didn't run yet
deals = repository.getDeals(searchQuery, maxDistInKm, userLatitude, userLongitude);
Что такое шаблон, в котором getDeals
не будет вызываться до завершения работы gotLocation
? Новичок в асинхронном программировании, поэтому любые другие советы приветствуются!
repository.getDeals
) вgotLocation
, чтобы он выполнялся только после фактического получения местоположения. - person a_local_nobody   schedule 14.12.2019