Passing Data From Search

I’m a little confused on how to pass data using Algolia.

I have 2 very similar instance - 1 using a regular TableView and another using a HitsTableView.

For the tableview I used…

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedDocumentId = locationArray[indexPath.row].documentId
    self.performSegue(withIdentifier: "goToLocationProfileSegue", sender: self)
}

// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let locationTableVC = segue.destination as? LocationTableViewController {
        locationTableVC.documentId = selectedDocumentId!
    }
}

And I tried to do the same thing with the HitsTableView controller. When I print the ID it comes out but it says unexpectedly found nil whenever I tried to pass it…

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, containing hit: [String : Any]) {
    let selectedId = hit["objectID"] as? String
    selectedDocumentId = selectedId
    self.performSegue(withIdentifier: "searchToLocationSegue", sender: nil)
}

// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let locationTableVC = segue.destination as? LocationTableViewController {
        locationTableVC.documentId = selectedDocumentId!
    }
}

Hey @phil.manning11,

hmm this is not supposed to happen. What I would do to debug is to put a breakpoint at the selectedId level, and see the value of it. Maybe hit["objectID"] is not a string for example but an integer? From the docs:

The value you provide for objectIDs can be an integer or a string.

Let me know what you find out!

Hey @guy.daher,

Thanks for the info.

I checked in Algolia and the objectId for my values is definitely a String. I put a breakpoint at the selectedId in didSelectRow and it would still crash at the prepareForSegue.

Then in prepareForSegue I put a static value and tested again with the breakpoints. This time is showed at the breakpoints that it was finding the correct value so would seem everything is working.

Does this means prepareForSegue is actually called first? And therefore it doesn’t have time to find the selectedId in didSelectRow?

Here is the error at prepare for segue…

Hey @phil.manning11,

If you check the debug window, we can see that most of the variables are nil or empty. After looking closely, my guess is that when you call performSegue, you are passing nil as the sender instead of self, and that’s why you’re not getting the correct value for selectedDocumentId

Thanks for checking this out @guy.daher

I did notice that and thought that could’ve been the issue so I changed it to self but it’s still finding nil for the selectedDocumentId

00 PM

I would check StackOverflow for this issue since this is an issue of passing data between controllers, not an Algolia issue. For example, check this thread. Let me know when you find out about the error :slight_smile:

Yeah it seems so - thanks @guy.daher

1 Like

So I decided to programatically present the view controller. I do believe it was related to the post @guy.daher linked to above. Since the cell was the connection of the segue to the new view controller prepareForSegue was actually getting called twice and the 2nd time being nil

func goToLocation() {
    let locationTableVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "locationProfile") as! LocationTableViewController
    locationTableVC.documentId = selectedDocumentId!
    self.present(locationTableVC, animated: true, completion: nil)
}
1 Like