gpt4 book ai didi

java - 从 Firestore 下载数据较晚

转载 作者:行者123 更新时间:2023-12-01 19:44:24 28 4
gpt4 key购买 nike

我想将一个数组与一个 fragment 打包在一起。 Parcelable 数组依赖于 firestore 数据检索。我的意思是数组的元素是从 Firestore 获取的。但是从 Firestore 检索数据已经很晚了,并且正在执行下一行代码并且正在打包空数组。如何使下一行等待从 Firestore 检索数据?

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {



private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
private static final int PERMISSIONS_REQUEST_ENABLE_GPS = 9001;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 9002;
private static final String TAG = "MainActivity";
private static final int ERROR_DIALOG_REQUEST = 9003;
private boolean mLocationPermissionGranted = false;

private List<User>mUserList=new ArrayList<>();
private ArrayList<UserLocation>mUserLocations=new ArrayList<>();

private FusedLocationProviderClient mFusedLocationProviderClient;
FirebaseFirestore mDb;

private GoogleMap mGoogleMap;
private UserLocation mUserPosition=null;
private LatLngBounds latLngBoundary;

private ClusterManager mClusterManager;
private MyClusterManagerRenderer mClusterManagerRenderer;
private ArrayList<ClusterMarker> mClusterMarkers=new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDb = FirebaseFirestore.getInstance();

initUser();//in this method the data retrieving is implemented

if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
MapFragment mapFragment = new MapFragment();
Bundle bundle=new Bundle();
bundle.putParcelableArrayList(getString(R.string.userlocations_array), mUserLocations);
mapFragment.setArguments(bundle);


getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, mapFragment).commit();
}

}

private void initUser() {
User user=new User();
user.setEmail("nobeld@gmail.com");
user.setResponse("ok");
user.setUser("student");
user.setUserId("5");
user.setUserName("nobel");
((UserClient)(getApplicationContext())).setUser(user);
mUserList.add(user);
User user1=new User();
user1.setEmail("rahuld@gmail.com");
user1.setResponse("ok");
user1.setUser("student");
user1.setUserId("6");
user1.setUserName("rahul");
User user2=new User();
user2.setEmail("milond@gmail.com");
user2.setResponse("ok");
user2.setUser("student");
user2.setUserId("7");
user2.setUserName("milon");
mUserList.add(user1);
mUserList.add(user2);
for(User u: mUserList){
getUserLocation(u);
//firestore is implemented inside this method
Log.d(TAG, "initUser: in user array");
}



}

private void setCameraView(){
if(mUserPosition!= null){
Log.d(TAG, "setCameraView: user position got");
double bottomboundary=mUserPosition.getGeo_point().getLatitude()-.05;
double leftboundary = mUserPosition.getGeo_point().getLongitude()-.05;
double upboundary = mUserPosition.getGeo_point().getLatitude()+.05;
double rightboundary = mUserPosition.getGeo_point().getLongitude()+.05;
latLngBoundary=new LatLngBounds(new LatLng(bottomboundary,leftboundary),
new LatLng(upboundary,rightboundary));
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBoundary,0));

}else {
Log.d(TAG, "setCameraView: user position is null");
}
}
private void getUserLocation(User user){
Log.d(TAG, "getUserLocation: ");
DocumentReference locationRef=mDb.collection(getString(R.string.collection_user_location_student))
.document(user.getUserId());
locationRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
if(task.getResult().toObject(UserLocation.class)!= null){
Log.d(TAG, "Location onComplete: ");
UserLocation u=task.getResult().toObject(UserLocation.class);
mUserLocations.add(u);
//here adding the elements to array.

}else {
Log.d(TAG, "onComplete: result is empty");
}
}
}
});

}

}

最佳答案

因为数据仅在 onComplete() 方法内可用,因为它是异步行为,所以当您尝试将 mUserLocations 列表添加到 Bundle 对象,数据尚未完成从数据库的加载,这就是不可访问的原因(列表为空)。解决此问题的一个快速方法是移动以下代码行:

Bundle bundle=new Bundle();
bundle.putParcelableArrayList(getString(R.string.userlocations_array), mUserLocations);
mapFragment.setArguments(bundle);

在以下代码行之后的 onComplete() 方法内部:

//here adding the elements to array.

What to do to make the next lines wait until the data is retrieved from the Firestore??

如果您想在该方法之外使用mUserLocations,我建议您查看此post中我的答案的最后部分。 其中我解释了如何使用自定义回调来完成它。您还可以看看这个video 为了更好地理解。

关于java - 从 Firestore 下载数据较晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54026376/

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