MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Can I pre-populate relational data (e.g. RealmList<Object>) into Realm

Is it possible to import relational data into Realm? My data model for ObjectA has a one-to-many relationship using RealmList. This is for my Swift iOS app.

I pre-populated data using Realm Studio’s import from CSV. Most of my data model is made up of Strings and Ints. But I’m not sure how to represent a List<> datatype in CSV.

I’m creating custom code to create this data in my app at runtime. But it’s time consuming, and this strikes me as basic functionality. There has to be a better way!

Asking is it possible is pretty vague as yes, it’s possible. However, without seeing some code or examples of your data it’s going to be hard to say how or what advice to give.

For example, if you want to read in ‘relational data’ you could write an app to read that data, populate your realm objects and save it to realm.

Representing relational data in a flat file usually has some common keys that associate that data. For example people and dogs. The people file may look like this; persons id, name and favorite food

1234,jay,pizza
5678,cindy,steak

then the dogs file would look like this with the dog id, dog name and then the owners id

1111,rover,1234
2222,spot,1234
3333,scraps,5678

jay owns rover and spot and cindy owns scraps.

The realm object would be

class PersonClass: Object {
   @objc dynamic var person_id = ""
   @objc dynamic var person_name = ""
   @objc dynamic var fav_food = ""
   let dogList = List<DogClass>()
}

Thanks Jay for the feedback! I took your suggestion and ran into a problem…

My end goal is to pre-populate my app with data by bundling a Realm file. But I’m doing backflips to get data in the correct format. I created my Realm from CSVs, and made a separate app to create relationships (per your input). New problem is: Realm Studio converts my variables into optionals during the import…

Int?, String?, String?
1234, jay, pizza
5678, cindy, steak

Is there a way to prevent Realm Studio from doing this? Or is there a good way to get them back into non-optionals? I wrote a method for this, but instantiating objects with ‘id = RealmOptional()’ seems to overwrite any pre-existing value with a nil value.

So I’m stuck until I can figure out a way around this! Help me Obi Wan :slight_smile:

In my answer, I was referring to structuring your data to be read in as a flat file where you are creating the app to read that flat file. Doing it in code would allow you to generate relationships in whatever ways you want (1-1, 1-Many, Many-Many)

If you’re importing data in Realm Studio that’s different and what you’re showing is not the correct format for importing.

I will point you to my answer on Stack Overflow

So if we have a simple person class as in my example

class PersonClass: Object {
   @objc dynamic var person_id = ""
   @objc dynamic var person_name = ""
   @objc dynamic var fav_food = ""
}

The Realm object name is PersonClass , so the imported file name needs to match

PersonClass.csv

along with that the first line of the file needs to match the classes property names, comma separated so the import file would look like this

person_id,person_name,fav_food
1234,jay,pizza
5678,cindy,steak