How to pass data between 2 Controllers


Do we use segues to link between ViewControllers in Realm?
That’s what I tried to do so far #1 Vc1

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let index = model[indexPath.row]
        performSegue(withIdentifier: "toVC2", sender: index)
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let tovc2 = segue.destination as? ViewController2 {
            tovc2.model_2 = sender as? Results<Model>


    var model_2: Results<Model>!


I don’t think that this is a good approach.

It will be way better, if you don’t put your model here, rather than just the model id, and query in the next VC.


Thanks for you respond,
but what do you mean by model id? can you give me example according to the code above please? I’m still newbie


Look up “database primary key”. A primary key is a property (or column) that uniquely identifies a given instance of an object. In SQL databases the primary key is generally an Integer, though more modern databases (Realm, Firebase, iCloud) tend to use UUID strings.

@freeubi is saying that you should pass the primary key (generally a model’s “id” property) to the next view controller, so that view controller itself can query the database for the proper instance of the model.

That being said, Realm’s own tutorials pass the actual object, not the object’s id I don’t think you’re wrong to pass the object, but if you’re not sure how it works try going through the tutorial. It’s very thorough and you’ll have a perfectly working ToDo list app by the end of it.


Does this logic is working on the cloud server only? or for the local database as well?


Local too.
What you are trying to do is okay for checking things up but not for a production app.


@freeubi Can you tell us a bit more why passing an object is not a good idea in a production app?

Realm objects are live auto-updating views into your data so suppose, for example, we have a master detail view app. The master shows a list of users which can be clicked on, the detail shows the users details.

When a user is clicked, segue or display a detail view, or a sheet, of that users name, email address etc. What would be the benefit or effect of hitting the drive again to load that data instead of passing that object to get that data from?