1

estou quebrando a cabeça com uma aplicação segundo alguns exemplos do Brian Lets Build That App , adaptando seus videos para as minhas necessidade. Estou baixando algumas imagens do Firebase e atualizando uma collectionview. O que estou tendo problema é não saber qual o melhor lugar para colocar a função completionHandler([appPhotos]), já tentei de tudo ....

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

O Resultado deveria ser :

DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
OUT FOR
CALLING

ou

OUT FOR
CALLING
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
DOWNLOADED
CALLING

Obrigado por ajudar.

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .