本文介绍了完成异步任务的火力地堡与斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的我想写这将返回我的所有工人数组的功能,但它是从火力检索数据之前返回的
这是我的code:
FUNC getWorkersList() - GT; ([工人])
{
让workersInfoRef = ref.childByAppendingPath(\"countries/\\(userCountry)/cities/\\(userCity)/workers/\\(workFieldToRecieve)/\") 变种workerList = [工人]()
workersInfoRef.queryOrderedByChild(name)的observeSingleEventOfType(.value的,withBlock:{(快照)在 打印(snapshot.childrenCount)
休息的snapshot.children.allObjects作为! [FDataSnapshot] { 让workerInfo =工人(UID:rest.value [UID作为字符串名称:!rest.value [名称]为String,城市:!rest.value [城市】作为字符串,职业:休息.value的[专业]为String,电话:rest.value [电话]为String,电子邮件:rest.value [电子邮件]为String,国家:rest.value [国家作为!字符串)
workerList.append(workerInfo) }
}){(误差)
打印(error.description)
} 打印(workerList.count)
返回workerList}
解决方案
这不是测试...我只是codeD起来就飞给你的总体思路
添加完成块/回调到你的函数...
FUNC getWorkersList(回调:((数据:[工人]) - GT;无效)){
让workersInfoRef = ref.childByAppendingPath(\"countries/\\(userCountry)/cities/\\(userCity)/workers/\\(workFieldToRecieve)/\") 变种workerList = [工人]()
workersInfoRef.queryOrderedByChild(名称)
.observeSingleEventOfType(.value的,withBlock:{(快照)在 打印(snapshot.childrenCount)
休息的snapshot.children.allObjects作为! [FDataSnapshot] { 让workerInfo =工人(UID:rest.value [UID作为字符串名称:!rest.value [名称]为String,城市:!rest.value [城市】作为字符串,职业:休息.value的[专业]为String,电话:rest.value [电话]为String,电子邮件:rest.value [电子邮件]为String,国家:rest.value [国家作为!字符串)
workerList.append(workerInfo) }
打印(workerList.count)
回调(workerList) }){(印刷错误)(error.description)}
}
在传递完成块..
getWorkersList(回调:{(数据:[工人]) - GT;无效的
打印(数据)
})
I am trying to write a function that will return an array of all my workers, but it is returning before retrieving the data from firebase
here is my code:
func getWorkersList() -> ([Worker])
{
let workersInfoRef = ref.childByAppendingPath("countries/\(userCountry)/cities/\(userCity)/workers/\(workFieldToRecieve)/")
var workerList = [Worker]()
workersInfoRef.queryOrderedByChild("name").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [FDataSnapshot]{
let workerInfo = Worker(uid: rest.value["uid"] as! String, name: rest.value["name"] as! String, city: rest.value["city"] as! String, profession: rest.value["profession"] as! String, phone: rest.value["phone"] as! String, email: rest.value["email"] as! String, country: rest.value["country"] as! String)
workerList.append(workerInfo)
}
}) { (error) in
print(error.description)
}
print(workerList.count)
return workerList
}
解决方案
This is not tested... I just coded it up on the fly to give you the general idea
add a completion block/callback to your function...
func getWorkersList(callback: ((data:[Worker]) ->Void )) {
let workersInfoRef = ref.childByAppendingPath("countries/\(userCountry)/cities/\(userCity)/workers/\(workFieldToRecieve)/")
var workerList = [Worker]()
workersInfoRef.queryOrderedByChild("name")
.observeSingleEventOfType(.Value, withBlock: { (snapshot) in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [FDataSnapshot]{
let workerInfo = Worker(uid: rest.value["uid"] as! String, name: rest.value["name"] as! String, city: rest.value["city"] as! String, profession: rest.value["profession"] as! String, phone: rest.value["phone"] as! String, email: rest.value["email"] as! String, country: rest.value["country"] as! String)
workerList.append(workerInfo)
}
print(workerList.count)
callback(workerList)
}) { (error) in print(error.description) }
}
pass in the completion block..
getWorkersList(callback: { (data:[Worker]) -> Void in
print(data)
})
这篇关于完成异步任务的火力地堡与斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!