I have a simple function loading date from firebase.
我有一个来自firebase的简单函数加载日期。
func loadFromFireBase() -> Array? {
var songArray:Array = []
ref.observe(.value, with: { snapshot in
//Load songArray
})
if songArray.isEmpty {
return nil
}
return songArray
}
Currently this function returns nil always even though there is data to load. It does this because it doesn't ever get to the perform the completion block where it loads the array before the function returns. I'm looking for a way to make the function only return once the completion block has been called but I can't put return in the completion block.
目前,即使有要加载的数据,此函数也会返回nil。它这样做是因为它不会在函数返回之前执行完成块,它会加载数组。我正在寻找一种方法,只有在调用完成块后才能返回该函数,但我无法在完成块中返回。
11
(Variations on this question come up constantly on SO. I can never find a good, comprehensive answer, so below is an attempt to provide such an answer)
(关于这个问题的变化经常出现在SO上。我永远找不到一个好的,全面的答案,所以下面是尝试提供这样的答案)
You can't do that. Firebase is asynchronous. Its functions take a completion handler and return immediately. You need to rewrite your loadFromFirebase function to take a completion handler.
你不能这样做。 Firebase是异步的。它的函数采用完成处理程序并立即返回。您需要重写loadFromFirebase函数以获取完成处理程序。
I have a sample project on Github called Async_demo (link) that is a working (Swift 3) app illustrating this technique.
我在Github上有一个名为Async_demo(link)的示例项目,它是一个工作(Swift 3)应用程序,说明了这种技术。
The key part of that is the function downloadFileAtURL
, which takes a completion handler and does an async download:
关键部分是函数downloadFileAtURL,它接受一个完成处理程序并执行异步下载:
typealias DataClosure = (Data?, Error?) -> Void
/**
This class is a trivial example of a class that handles async processing. It offers a single function, `downloadFileAtURL()`
*/
class DownloadManager: NSObject {
static var downloadManager = DownloadManager()
private lazy var session: URLSession = {
return URLSession.shared
}()
/**
This function demonstrates handling an async task.
- Parameter url The url to download
- Parameter completion: A completion handler to execute once the download is finished
*/
func downloadFileAtURL(_ url: URL, completion: @escaping DataClosure) {
//We create a URLRequest that does not allow caching so you can see the download take place
let request = URLRequest(url: url,
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 30.0)
let dataTask = URLSession.shared.dataTask(with: request) {
//------------------------------------------
//This is the completion handler, which runs LATER,
//after downloadFileAtURL has returned.
data, response, error in
//Perform the completion handler on the main thread
DispatchQueue.main.async() {
//Call the copmletion handler that was passed to us
completion(data, error)
}
//------------------------------------------
}
dataTask.resume()
//When we get here the data task will NOT have completed yet!
}
}
The code above uses Apple's URLSession
class to download data from a remote server asynchronously. When you create a dataTask
, you pass in a completion handler that gets invoked when the data task has completed (or failed.) Beware, though: Your completion handler gets invoked on a background thread.
上面的代码使用Apple的URLSession类以异步方式从远程服务器下载数据。当您创建dataTask时,您传入一个完成处理程序,该处理程序在数据任务完成(或失败)时被调用。但要注意:您的完成处理程序在后台线程上被调用。
That's good, because if you need to do time-consuming processing like parsing large JSON or XML structures, you can do it in the completion handler without causing your app's UI to freeze. However, as a result you can't do UI calls in the data task completion handler without sending those UI calls to the main thread. The code above invokes the entire completion handler on the main thread, using a call to DispatchQueue.main.async() {}
.
这很好,因为如果您需要执行耗时的处理(如解析大型JSON或XML结构),您可以在完成处理程序中执行此操作,而不会导致应用程序的UI冻结。但是,因此,如果不将这些UI调用发送到主线程,则无法在数据任务完成处理程序中执行UI调用。上面的代码使用对DispatchQueue.main.async(){}的调用来调用主线程上的整个完成处理程序。
Back to the OP's code:
回到OP的代码:
I find that a function with a closure as a parameter is hard to read, so I usually define the closure as a typealias.
我发现一个带闭包作为参数的函数很难读,因此我通常将闭包定义为一个类型。
Reworking the code from @Raghav7890's answer to use a typealias:
从@ Raghav7890的答案重新编写代码以使用类型:
typealias SOngArrayClosure= (Array?) -> Void
func loadFromFireBase(completionHandler: @escaping SongArrayClosure) {
ref.observe(.value, with: { snapshot in
var songArray:Array = []
//Put code here to load songArray from the FireBase returned data
if songArray.isEmpty {
completionHandler(nil)
}else {
completionHandler(songArray)
}
})
}
I haven't used Firebase in a long time (and then only modified somebody else's Firebase project), so I don't remember if it invokes it's completion handlers on the main thread or on a background thread. If it invokes completion handlers on a background thread then you may want to wrap the call to your completion handler in a GCD call to the main thread.
我很长一段时间没有使用Firebase(然后只修改了别人的Firebase项目),所以我不记得它是在主线程还是后台线程上调用它的完成处理程序。如果它在后台线程上调用完成处理程序,那么您可能希望在对主线程的GCD调用中将调用包装到完成处理程序中。
Based on the answers to this SO question, it sounds like Firebase does it's networking calls on a background thread but invokes it's listeners on the main thread.
根据这个SO问题的答案,听起来像Firebase在后台线程上进行网络调用,但在主线程上调用它的侦听器。
In that case you can ignore the code below for Firebase, but for those reading this thread for help with other sorts of async code, here's how you would rewrite the code to invoke the completion handler on the main thread:
在这种情况下,您可以忽略Firebase下面的代码,但对于那些阅读此线程以获取其他种类异步代码的帮助的人,以下是如何重写代码以调用主线程上的完成处理程序:
typealias SOngArrayClosure= (Array?) -> Void
func loadFromFireBase(completionHandler:@escaping SongArrayClosure) {
ref.observe(.value, with: { snapshot in
var songArray:Array = []
//Put code here to load songArray from the FireBase returned data
//Pass songArray to the completion handler on the main thread.
DispatchQueue.main.async() {
if songArray.isEmpty {
completionHandler(nil)
}else {
completionHandler(songArray)
}
}
})
}
2
Making Duncan answer more precise. You can make the function like this
让邓肯的回答更精确。你可以做这样的功能
func loadFromFireBase(completionHandler:@escaping (_ songArray: [Song]?)->()) {
ref.observe(.value) { snapshot in
var songArray: [Song] = []
//Load songArray
if songArray.isEmpty {
completionHandler(nil)
}else {
completionHandler(songArray)
}
}
}
You can return the songArray in a completion handler block.
您可以在完成处理程序块中返回songArray。