我有一个名为 User 的类,它具有使用 GeoFire 获取附近所有食品卡车的功能。我使用 observeReadyWithBlock 获取 GeoFire 返回的卡车 ID,并使用 Firebase 获取其余信息。但是,当我在添加名称和描述后从我的卡车对象数组中访问其中一辆卡车时,看起来 xCode 告诉我该数组为空。
我计划在其他控制器类中使用这组附近的卡车,以填充表格,向用户显示所有附近的卡车和一些基本信息。
如何正确填充我的卡车数组,根据下面的代码我可能会出错。非常感谢!
func getNearbyTrucks(){
//Query GeoFire for nearby users
//Set up query parameters
let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
let circleQuery = geoFire.queryAtLocation(center, withRadius: 100)
circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in
let newTruck = Truck()
newTruck.id = key
newTruck.currentLocation = location
self.nearbyTrucks.append(newTruck)
}) //End truckQuery
//Execute code once GeoFire is done with its' query!
circleQuery.observeReadyWithBlock({
for truck in self.nearbyTrucks{
ref.childByAppendingPath("users/\(truck.id)").observeEventType(.Value, withBlock: { snapshot in
print(snapshot.value["name"] as! String)
truck.name = snapshot.value["name"] as! String
truck.description = snapshot.value["selfDescription"] as! String
let base64String = snapshot.value["profileImage"] as! String
let decodedData = NSData(base64EncodedString: base64String as String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
truck.photo = UIImage(data: decodedData!)!
})
}
}) //End observeReadyWithBlock
print(nearbyTrucks[0].id)
//This line gives the error that the array index is out of range
}