Search code examples
swiftprepared-statementsegue

How to fix delayed data on prepare segue?


My data got delayed 1 time when I want to pass the data in my tableview and pass it to another viewcontroller

i'm using prepare for segue.

now, in order to get the right data i need go back to the table view and press the same row

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toRequestCorrection"{
            let destinationVC = segue.destination as! RequestCorrectionViewController
            if let indexPath = tableView.indexPathForSelectedRow{
                destinationVC.Shift = self.correction["Shift"].stringValue
                destinationVC.LogDate = self.correction["LogDate"].stringValue
                destinationVC.logIn = self.correction["ActualLogIn"].stringValue
                destinationVC.logOut = self.correction["ActualLogOut"].stringValue
                destinationVC.breakEnd = self.correction["ActualBreakEnd"].stringValue
                destinationVC.breakStart = self.correction["ActualBreakStart"].stringValue
                destinationVC.overTimeIn = self.correction["ActualOverTimeIn"].stringValue
                destinationVC.overTimeOut = self.correction["ActualOverTimeOut"].stringValue
                destinationVC.transactionStatusID = self.correction["TransactionStatusID"].intValue
            }
        }
    }

it's should pass the data on the row right after i pressed the row


Solution

  • Additionally, you do not have to use segue, you can instantiate your view controller inside didSelectRowAt method with following code without prepareForSegue.

    EDIT: You didn't indicate it's an async task. So, I am using Alamofire with completion handler. I think it will be useful for you.

    typealias yourCompletionHandler = (Data?, _ message: String?, _ errorStatusCode: Int?) -> Void
    func yourAsyncTask(handler: @escaping yourCompletionHandler) {
        let parameters: [String: Any] = ["unitId": 4124]
        let url = ""
        Alamofire.request(url, method: .get, parameters: parameters)
            .responseObject { (response: DataResponse<BaseListResponseParser<YourModel>>) in
                switch response.result {
                case .success:
                    if let result = response.result.value {
                      handler(result.listItems, nil, nil)
                    }
                case .failure(let error):
                    print(error)
                }
        }
    } 
    
    
    yourAsyncTask { [weak self] (yourModel, error, _) in
            DispatchQueue.main.async {
                guard let destination = UIStoryboard(name: "Main", bundle: nil)
                    .instantiateViewController(withIdentifier: "ViewControllerId") as? RequestCorrectionViewController else { return }
                destination.Shift = yourModel.shift
                navigationController?.pushViewController(destination, animated: true)
            }
        }
    

    Using that way you do not need to create segue or prepareForSegue method.