java - 从 Firestore 下载数据延迟
问题描述
我想array
用一个片段包裹一个。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.
怎么做才能让下一行等到从 Firestore 中检索到数据?
如果您想在外部使用该方法,我建议您从这篇文章mUserLocations
中查看我的 anwser 的最后一部分,其中我已经解释了如何使用自定义回调来完成它。您也可以观看此视频以更好地理解。
推荐阅读
- class - 颤振的语法含义
- php - Wordpress 的 WooCommerce 如何使用回调属性和获取产品规范
- python - Python中的Agrupate变量
- javascript - 我需要发送多个 ajax 请求并使用 jQuery 比较数据。这段代码有效,但太慢了。如何优化它?
- json - 使用 powershell 删除变量内的循环
- c - 关于 for 循环中的函数调用的问题
- spring - Thymeleaf:Bean 名称“#ids”的 BindingResult 和普通目标对象都不能用作请求属性
- c++ - 在 C++ 中使用宏进行运算符重载
- windows - 没有足够的内存来执行请求的操作请增加 xml
- django - 如何检查媒体文件夹中是否存在文件?