1

I am trying to reload the data after the function is completed:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if let presentingVC = presentingViewController as? DealsViewController {

 //checks if presenting VC's city is the same as original, doesn't run if it is      
 if presentingVC.city != self.currentCity {
        DispatchQueue.main.async {
            //reloads the data but before completion of the below function "getDealInfo"
            presentingVC.DealsTableView.reloadData()
            presentingVC.DealsTableView.layoutIfNeeded()
        }
    }
//checks if presenting VC's city is the same as original, doesn't run if it is        
if presentingVC.city != self.currentCity {
        presentingVC.city = self.currentCity
        presentingVC.deleteAllDeals()
        //this is the function that gets called
        presentingVC.getDealInfo()
        presentingVC.cityOutlet.setTitle(self.currentCity,for: .normal)
        }
       
    }
    tableView.deselectRow(at: indexPath, animated: true)
    searching = false
    self.dismiss(animated: true, completion: nil)
    searchBar.resignFirstResponder()
}

As you can see, I call the reload function after returning to the previous view controller for new information that is supplied. However, the tableview updates prior to receiving the new values. As a result, my tableView is empty, however my values are stored in my arrays.

getDealInfo is Asynchronous:

    func getDealInfo() {
    let query = PFQuery(className: "Deal")
    query.whereKey("City", equalTo: city)
    query.order(byDescending: "Priority")
    query.findObjectsInBackground(block: { (objects: [PFObject]?,error: 
    Error?) in
    if let objects = objects {
        for object in objects {
            self.dealsArray.append(object["Deal"] as! String)
              }
            }
       }
    })
 }
1
  • Are those synchronous methods or async? If they're synchronous just reorder your code. I don't know why you have the same if statement twice Commented Jul 23, 2020 at 21:35

2 Answers 2

1

Add a completion handler to getDealInfo in order to be notified when it finishes:

func getDealInfo(_ completionHandler: @escaping () -> Void) {
    //Do async work
    completionHandler()
}

Then in the calling code:

presentingVC.getDealInfo {
    self.presentingVC.DealsTableView.reloadData()
}

If the completion handler isn't being called in the main thread then you want to use DispatchQueue.main.async like you're currently doing so the call to reloadData is done in the main thread.

Sign up to request clarification or add additional context in comments.

Comments

0

Are those synchronous methods or async? If they're synchronous just reorder your code. I don't know why you have the same if statement twice. Just combine it into one and call reload data after you run your function

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if let presentingVC = presentingViewController as? DealsViewController {
//checks if presenting VC's city is the same as original, doesn't run if it is        
if presentingVC.city != self.currentCity {
        presentingVC.city = self.currentCity
        presentingVC.deleteAllDeals()
        //this is the function that gets called
        presentingVC.getDealInfo()
        presentingVC.cityOutlet.setTitle(self.currentCity,for: .normal)
         DispatchQueue.main.async {
            //reloads the data but before completion of the below function "getDealInfo"
            presentingVC.DealsTableView.reloadData()
            presentingVC.DealsTableView.layoutIfNeeded()
        }
        }
       
    }
    tableView.deselectRow(at: indexPath, animated: true)
    searching = false
    self.dismiss(animated: true, completion: nil)
    searchBar.resignFirstResponder()
}

If they're asynchronous then you'll need to look at providing some kind of completion block. But without knowing if presentingVC.getDealInfo() is async or not it's hard to know what to suggest. I feel like we're missing some more vital code here

1 Comment

thanks for responding so soon. It is asynchronous. How would I show such a completion block?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.