1
I’m racking my brain with an app based on a few examples of Brian Lets Build That App , adapting your videos to my needs.
I’m downloading some images from Firebase and updating a collectionview. What I’m having trouble with is not knowing the best place to put the job completionHandler([appPhotos])
, I’ve tried everything ....
Controllerdetail.Swift
AppPhotos.fetchePhotosApp(pacienteID: self.paciente_key) { (appPhotos) in
self.appPhotos = appPhotos
self.collectionView?.reloadData()
print("CALLING")
}
Models.Swift
static func fetchePhotosApp(pacienteID: String, completionHandler: @escaping ([AppPhotos]) -> ()) {
let user = FIRAuth.auth()?.currentUser
let appPhotos = AppPhotos() //
var apps = [App]() //
FIRDatabase.database().reference().child((user?.uid)!).child("photos").child(pacienteID).observeSingleEvent(of: .value, with: { (snapshot) in
var newPhotos = [Photo]()
for itemSnapShot in snapshot.children {
let item = Photo(snapshot: itemSnapShot as! FIRDataSnapshot)
newPhotos.append(item)
}
let photos = App()//
var dataConsulta = [Int]()
for index in 0..<newPhotos.count {
if !dataConsulta.contains(newPhotos[index].data_consulta!) {
dataConsulta.append(newPhotos[index].data_consulta!)
}
appPhotos.data_consulta = newPhotos[index].data_consulta_text
let url = URL(string: newPhotos[index].photo_original!)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
photos.photo_original = UIImage(data: data!)
apps.append(photos)
print("DOWNLOADED")
}).resume()
}
print("OUT FOR")
appPhotos.apps = apps
DispatchQueue.main.async(execute : {
completionHandler([appPhotos])
})
}, withCancel: nil)
}
Result
OUT FOR
CALLING
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
The Result should be :
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
OUT FOR
CALLING
or
OUT FOR
CALLING
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
CALLING
Thanks for helping.