如何在Firebase完成下载后直接执行代码? [英] How to execute code directly after Firebase finishes downloading?

查看:119
本文介绍了如何在Firebase完成下载后直接执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码需要在Firebase完成下载之前立即执行。问题是这个代码总是在下载完成之前运行。

  if currentVersionNumber< newVersionNumber {
print(Feed:button donwloading cards)
//self.databaseButton.setTitle(\"Downloadloading New Cards ...,for:.normal)
ref.observe( .value与:{快照
print(Feed:从firebase检查新卡)
用于snapshot.children中的项目{
//以最大允许大小下载内存($ 1 $ 1024 * 1024字节)
cardRef.data(withMaxSize:1 * 1024 * 1024){(数据,错误) - >在
中无效if(error!= nil){
//呃 - 哦,发生错误!
print(Feed:error occured)
print(error)
} else {
// Data for for images / island.jpg返回
cards.imageName = data!
print(Feed:downloaded \(cards.name))
}
}
//添加到更新的卡片列表

updateCards.append(cards);
$ b})

} else {
print(Feed:cards are up to date。\(currentVersionNumber))
}

))

此代码从Firebase数据库下载需要的项目在它完成之前,它将运行任何代码。这些网络请求如何使我可以选择执行一个代码块,一旦下载完成?

解决方案

这些网络请求运行异步,以便任何后面的代码将继续运行,而网络请求正在完成。

你应该在内部闭包内移动updateCards.append(卡),所以它不会直到第二个闭包完成时才会被调用,如果在完成时需要运行其他代码,则可以将其移至此函数中,或者使用闭包和完成处理程序来确保所有网络请求都已完成

$ p $ get $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $你需要完成后

$ b $ func getCardData(_ completion:() - >()){
print(Feed:button donwloading cards)
//self.databaseButton.setTitle(\"Downloadloading New Cards ...,for:.normal)
ref.observe(.value, (快照在
print(Feed:检查Firebase中的新卡片)
用于snapshot.children中的项目{
//在内存中下载最大允许大小为1MB( 1 * 1024 * 1024字节)
cardRef.data(withMaxSize:1 * 1024 * 1024){(data,error) - >无效
if(error!= nil){
//呃 - 哦,发生了一个错误!
print(Feed:error occured)
print(error)
completion()//这是您通常会抛出错误的地方,或者有一个可以接受可选错误的闭包通过了解它失败
} else {
//返回images / island.jpg的数据
cards.imageName = data!
print(Feed:downloaded \(cards.name))
updateCards.append(cards);
completion()//现在你知道所有的网络请求都完成了




$ b code>


I have code that needs to be executed as soon as Firebase finishes downloading what I tasked it to download before hand. The issue is that this code is always running before the download is complete.

if currentVersionNumber < newVersionNumber {
            print("Feed: button donwloading cards")
            //self.databaseButton.setTitle("Downloading New Cards...", for: .normal)
            ref.observe(.value, with: { snapshot in
                print("Feed: Checking for new cards from firebase")
                for item in snapshot.children {
                    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
                    cardRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
                        if (error != nil) {
                            // Uh-oh, an error occurred!
                            print("Feed: error occured")
                            print(error)
                        } else {
                            // Data for "images/island.jpg" is returned
                            cards.imageName = data!
                            print("Feed: downloaded \(cards.name)")
                        }
                    }
                    // add to updated list of cards

                    updateCards.append(cards);
                }
            })

        } else {
             print("Feed: cards are up to date. \(currentVersionNumber)")
        }

    })

This code downloads the items I want from Firebase Database, but will run any code after it, before it is complete. How do I make it so that I can choose to execute a code block as soon as the download finishes?

解决方案

these network requests run async so any code after them will continue to run while the network request is being completed.

you should move updateCards.append(cards) inside the inner closure so it doesn't get called until that second closure finishes, then if you have other code you need to run when this is complete you can either move it inside of this function or use a closure with a completion handler to make sure all network requests are completed before you run any more code the relies on the response.

getCardData { [weak self] in
// do whatever you need to do after completion
}

func getCardData(_ completion: () -> ()) {
print("Feed: button donwloading cards")
//self.databaseButton.setTitle("Downloading New Cards...", for: .normal)
ref.observe(.value, with: { snapshot in
    print("Feed: Checking for new cards from firebase")
    for item in snapshot.children {
        // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
        cardRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
            if (error != nil) {
                // Uh-oh, an error occurred!
                print("Feed: error occured")
                print(error)
                completion() // this is where you would normally throw an error or have a closure that accepts an optional error you would pass in to know it failed
            } else {
                // Data for "images/island.jpg" is returned
                cards.imageName = data!
                print("Feed: downloaded \(cards.name)")
                updateCards.append(cards);
                completion()// now you know all network requests are complete
            }
        }
     }
  })
}

这篇关于如何在Firebase完成下载后直接执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆