【发布时间】:2018-02-22 16:46:03
【问题描述】:
我从我的 fetch 函数中返回 DatabaseReference 和 Databasehandle 以便稍后分离监听器。但是,在 guard 语句的闭包内,我无法返回 (ref, handle),因为它位于句柄定义内。奇怪的是,如果我简单地输入return,Xcode 不会对我大喊大叫并且编译得很好。这是正确的吗?
我知道我可以改用 DatabaseReference? 和 Databasehandle? 并在保护语句中返回 (nil, nil)。但对我来说更有意义的是,无论获取是否成功,都应该返回一个引用和一个句柄。
func fetchQuestions(completion: @escaping (Question?)->()) -> (DatabaseReference, DatabaseHandle)
{
let ref = root.child("timeline").child(uid)
let handle = ref.observe(.childAdded, with: { (snapshot) in
var question: Question?
defer { completion(question) }
guard let dict = snapshot.value as? [String: Int] else {
return // Is this correct?
}
...
})
return (ref, handle)
}
【问题讨论】:
标签: swift firebase asynchronous firebase-realtime-database swift4