ฉันมีปัญหากับการเขียนโปรแกรมอะซิงก์บางอย่างภายในแอปพลิเคชัน Android ฉันมีโค้ดบางส่วนในคลาส ClientLocation
ที่รับค่า lat/lon ของผู้ใช้ แต่เมื่อได้โค้ดแล้ว จะต้องส่งโค้ดเหล่านั้นไปยังฟังก์ชัน เมื่อก้าวผ่านโค้ดฉันจะเห็นว่ามีการเรียกใช้การโทร 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