最近,我正在尝试从Firebase获取数据并将其发送给另一个视图控制器的表格视图。但由于AsyncTask处理不当,我失败了。请建议/告诉我如何解决这个问题。
以下是Firebase here中的表格。我想得到酒店名称的位置是' uttara'。
我用谷歌搜索尝试了很多,但它不能正常工作。我确定我的错误放置AsyncTask和闭包。
@IBAction func SearchActionInitiated(sender: UIButton)
{
dataTransfer(){ () -> () in
self.performSegueWithIdentifier("SearchResultPage", sender: self )
}
}
func dataTransfer(completed: backgroundTaskCompleted){
let BASE_URL_HotelLocation = "https://**************.firebaseio.com/Niloy"
let ref = FIRDatabase.database().referenceFromURL(BASE_URL_HotelLocation)
ref.queryOrderedByChild("location").queryStartingAtValue("uttara").observeEventType(.Value, withBlock: { snapshot in
if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
for child in result {
let downloadURL = child.value!["image"] as! String;
self.storage.referenceForURL(downloadURL).dataWithMaxSize(25 * 1024 * 1024, completion: { (data, error) -> Void in
let downloadImage = UIImage(data: data!)
let h = Hotel()
h.name = child.value!["name"] as! String
print("Object \(count) : ",h.name)
h.deal = child.value!["deal"] as! String
h.description = child.value!["description"] as! String
h.distance = child.value!["distance"] as! String
h.latestBooking = child.value!["latestBooking"] as! String
h.location = child.value!["location"] as! String
h.image = downloadImage!
self.HotelObjectArray.append(h)
dispatch_async(dispatch_get_main_queue(), {
completed();
});
})
}
}
else {
print("no results")
}
})
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "SearchResultPage")
{
let vc = segue.destinationViewController as! SearchResultPage
vc.arrayOfobject = HotelObjectArray
}
}
backgroundTaskCompleted
是封闭的地方。