Efficient way of passing data from DB to other ViewControllers


#1

In AppDelegate file I create a Realm instance and pass it to another ViewController as follows:

var config = Realm.Configuration()
config.readOnly = true
config.fileURL = URL(fileURLWithPath: Bundle.main.path(forResource: "Questions", ofType: "realm")!)
domainViewController.realm = try! Realm(configuration: config)

Now, in domainViewController I get array of objects, let’s say questions based on a specific user selection. This array is used to extract distinct values of a certain column which are displayed in yet another ViewController, called listViewController. Now, listViewController doesn’t need questions array yet but the ViewController that goes after listViewController needs it.

Is it better to pass Realm instance of database and then retrieve objects again or pass objects array (as it is already retrieved from the database) to another ViewController? Which is more efficient way of doing it?


#2

The question is a little vague as you can’t directly ‘load an array of objects’ from Realm. You can however get a Realm results object that behaves very much like an array.

From there you could work with the results directly or pull a subset of it’s data into an array. However, if you do that, those objects will ‘disconnect’ from Realm and not update.

A lot of it depends on your use case but once you have a Results object it will be updated automagically as the data changes that object represents, so it could be pretty easily passed to another view controller instead of hitting the disk again to create another instance of the same thing.