gpt4 book ai didi

java - 安卓/Java : Waiting until closure has completed running

转载 作者:行者123 更新时间:2023-12-01 19:23:06 25 4
gpt4 key购买 nike

我在 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);

gotLocation 完成运行之前不会调用 getDeals 的模式是什么?异步编程新手,欢迎任何其他提示!

最佳答案

这里:

boolean isFinished  = false

Thread {

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());
isFinished = true
}
};



while(true){

if(isFinished ) break:
}
ClientLocation clientLocation = new ClientLocation()
clientLocation.getLocation(getApplication().getApplicationContext(), locationResult);
deals = repository.getDeals(searchQuery, maxDistInKm,
userLatitude, userLongitude);

Thread.currentThread().interrupt()

}

关于java - 安卓/Java : Waiting until closure has completed running,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59338081/

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