random1234 random1234 - 1 year ago 169
Swift Question

Swift loading images from firebase

Does someone know why this is not working? The tableView is empty and not showing anything even though there are items in the database and storage. This worked fine before I implemented the loading of the images from storage which you will see at the bottom of this code that I have pasted in. The

food.append()
statement used to be outside the
storageRef.getData()
closure (since it didn't exist) however if I take it out now it won't be able to access
recipeImage
since it's declared within the closure. Is it not working because it's in the closure? If so how do I fix it?

let parentRef = Database.database().reference().child("Recipes")
let storage = Storage.storage()

parentRef.observe(.value, with: { snapshot in

//Processes values received from server
if ( snapshot.value is NSNull ) {

// DATA WAS NOT FOUND
print("– – – Data was not found – – –")

} else {

//Clears array so that it does not load duplicates
food = []

// DATA WAS FOUND
for user_child in (snapshot.children) {

let user_snap = user_child as! DataSnapshot
let dict = user_snap.value as! [String: String?]

//Defines variables for labels
let recipeName = dict["Name"] as? String
let recipeDescription = dict["Description"] as? String
let downloadURL = dict["Image"] as? String

let storageRef = storage.reference(forURL: downloadURL!)

storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in

let recipeImage = UIImage(data: data!)

food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
}
}
self.tableView.reloadData()
}
})

Answer Source

Move

self.tableView.reloadData()

after

food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download