私はGeoFireを使用して近くの食品トラックをすべて取得する機能を持つUserというクラスを持っています。私はobserveReadyWithBlockを使ってGeoFireから返されたトラックIDを取得し、Firebaseを使って残りの情報を取得しました。しかし、Truckオブジェクトの名前と説明を追加した後、私のトラックからトラックの1つにアクセスすると、xCodeが配列が空であるとわかります。Geofire/Firebaseを使用してユーザーのリストを組み立てる
他のコントローラークラスでこの近くのトラックの配列を使用して、近くのトラックのすべてを示すテーブルとユーザーに基本情報を入力することを計画しています。
トラックの配列を正しく挿入するにはどうすればよいですか、以下のコードに基づいて何が間違っているのでしょうか。どうもありがとう!
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
}
ありがとうございました。本当に本当に有益な、よかった! –