Saya mengalami masalah dengan beberapa pemrograman async dalam aplikasi Android. Saya memiliki beberapa kode di kelas ClientLocation
yang mendapatkan lintang/bujur pengguna, tetapi begitu saya mendapatkannya, saya harus meneruskannya ke suatu fungsi. Saat menelusuri kode saya dapat melihat bahwa panggilan repository.getDeals
dijalankan sebelum penutupan gotLocation
dimasukkan.
Inilah bagian penting dari kode:
...
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);
Apa pola di mana getDeals
tidak akan dipanggil sebelum gotLocation
selesai dijalankan? Baru mengenal pemrograman async, jadi tips lainnya dipersilakan!
repository.getDeals
) kegotLocation
sehingga kode tersebut hanya dapat dijalankan segera setelah lokasi benar-benar diterima. - person a_local_nobody   schedule 14.12.2019