0

I need to launch the funcion "caricaImmagine" after the function "caricaLavori". In the code is clear that caricaImmagine need that caricaLavori finish to append item to an array.

I've tried this but I don't know how to implement it.

Here's the "newMethod" (called in viewWillAppear):

 func newMethod(){
    print("login: \(login)")

    let userFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "UserEntity")

    do {
        fetchUsers = try moc.fetch(userFetch) as! [User]
    } catch {
        fatalError("Failed to fetch person: \(error)")
    }

    do{
        let users = try moc.fetch(userFetch)
        if(users.count > 0){
            print("utente connesso")
            tableview.delegate = self
            tableview.dataSource = self
            let d_email = fetchUsers.first!.email

            caricaLavori(datore_email: d_email!)
            for i in 0...lavori.count{
                caricaImmagine(id_lavoro: lavori[i].id)
            }

        }else{
            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "area_utente_segue", sender: self)
            }
        }
    }catch {}
}

If you want the other 2 functions I'll edit this post (both of them loads data from php script, both do an http request)

thank you guys

Community
  • 1
  • 1

1 Answers1

1

You say "both of them loads data from php script, both do an http request". That is a key piece of information.

On iOS you want to do network access asynchronously. That means that a function that submits a network requests submits that request and then returns immediately, before the request has even been sent to the remote server.

The solution is to write your methods to take a completion handler. The method invokes the completion handler once the task is complete.

Take a look at my answer in the thread below for code that includes a working example of using completion handlers:

Storing values in completionHandlers - Swift

Note that that post (And the sample app it links to) was written in Swift 2, and will need to be updated for Swift 3.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272