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?