如何创建一个函数,最终返回一些数据,调用者必须等待 [英] How to create a function that will eventually return some data and caller must wait
问题描述
这实际上是一个Swift语言类型的问题
我正在使用 Firebase 读取/写入数据
我想在swift中设计一个例程,最终返回。调用者必须等待例程完成,所以它不是一个异步的后台调用。理想情况下,调用者还应该能够:
$ b $ ol
确定成功或错误返回并处理返回值
如果调用函数耗时过长,超时等待
在这个功能里面,hold-up只是Firebase的一个事件。例如:
func finallyReturnFirebase(){
//某些东西
someObj.observeEventType(.ChildAdded,withBlock:
print(\(snapshot.key) - > \(snapshot.value))
if( snapshot.key ==foo){
//我们发现foo $ b $的值
if(snapshot.key ==bar){
//我们发现bar
})
//现在我们可以将foo和bar返回给调用者,或者如果我们没有
$ p
$ b有人可以突出显示Swift语言在设计这样的函数方面提供了什么,以及如何使用调用者?如果你需要编写异步调用的同步封装,你可以使用信号量(semaphores)。
即使你可以写这样的东西(我省略了某些东西,比如类型信息,所以它是一种类似于swift的伪代码,但是这应该足以实现这个想法):
func finallyReturnFirebase(){
let semaphore = dispatch_semaphore_create(0)//创建一个closed信号
var foo,bar / /变量,将保存您的返回值
someObj.observeEventType(.ChildAdded,withBlock:{
快照
print(\(snapshot.key) - > \\ \\(snapshot.value))
if(snapshot.key ==foo){
//我们发现foo $ b $的值
if(snapshot.key = =bar){
//找到bar
的值}
//设置foo和bar的值
foo = ...
bar = ...
dispatch_semaphore_signal(信号量)//增加信号量计数器
})
let timeout = dispatch_time(DISPATCH_TIME_NOW,DefaultTimeoutLengthInNanoSeconds)
如果dispatch_semaphore_wait(信号量,超时)!= 0 {//等待信号量conter大于0
print(timed out)
}
return foo,bar
}
This is actually a Swift Language type of question
I am using Firebase to read/write data
I would like to design a routine in swift that will return eventually. The caller must wait for the routine to complete, so its not a asynchronous, background call actually. Ideally the caller should also be able to:
Determine success or error returns and also process return values
Timeout on the wait if the calling function is taking too long
Inside the function the hold-up is merely an event to Firebase . For example:
func eventuallyReturnFirebase() { //some stuff someObj.observeEventType(.ChildAdded, withBlock: { snapshot in print("\(snapshot.key) -> \(snapshot.value)") if (snapshot.key == "foo") { // we found value for foo } if (snapshot.key == "bar") { // we found value for bar } }) //now we can return foo and bar back to caller or some error if we did not }
Can someone highlight what the Swift language offers in terms of designing such a function and how must the caller be used as well? Hopefully also addressing the 2 ideal conditions as well
解决方案If you need to write synchronous wrapper around asynchronous call - you can use semaphores.
I.e. you can write something like this (I'm omiting certain things like types information, so it's kind of swift-like pseudo-code, but this should be enough to get the idea):
func eventuallyReturnFirebase() { let semaphore = dispatch_semaphore_create(0) //creating a "closed" semaphore var foo, bar //variables that will hold your return values someObj.observeEventType(.ChildAdded, withBlock: { snapshot in print("\(snapshot.key) -> \(snapshot.value)") if (snapshot.key == "foo") { // we found value for foo } if (snapshot.key == "bar") { // we found value for bar } //setting values for foo and bar foo = ... bar = ... dispatch_semaphore_signal(semaphore) // incrementing semaphore counter }) let timeout = dispatch_time(DISPATCH_TIME_NOW, DefaultTimeoutLengthInNanoSeconds) if dispatch_semaphore_wait(semaphore, timeout) != 0 { // waiting until semaphore conter becomes greater than 0 print("timed out") } return foo, bar }
这篇关于如何创建一个函数,最终返回一些数据,调用者必须等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!