What is the proper way to seed initial data in. a newly created realm, some swift developers seemed to indicate a method that will only run once when the realm is first created but I can’t find anything in the docs to do this when defining an object schema. My only option right now is check the realm when I open it and if it has no data to populate it. Is there a better way?
Do you want to include a seeded Realm with your App or do you want to (as you mentioned) populate it with data when the app starts?
It’s really a design decision so it depends on your use case e.g. there is no “proper” way, it just depends on how you want to do it, amount of data, static or dynamic data etc.
Thanks for the reply, it seems like it would be difficult to manage these files if they are updated over time. My initial thought was to have config objects that get seeded in the realm. This makes them very human readable in the code for the developers manage over time. I’m think of having a method defined on the object schema that check if the realm is empty and if it is it inserts the seed config data.
The initial data could be in a bundled Realm instead of hundreds of lines of code in your app.
That makes it a snap to create the files for customer use based on what you’re included in your Bundled Realm.
In other words, imagine a game like World of Warcraft where characters start off with a base set of armor, stats and abilities.
Instead of including all of that code in your App to create a Realm and write that out, you could bundle them within a Realm and simply get the objects from the bundled Realm and write them out to the users Realm.
When you have 10 different character classes, a couple dozen types of armor (unless you’re a mage then your “armor” is a dress, but I digress) and different stats, having that set up in a pre-defined way is easily maintainable and can be modified without having to re-compile your app.
Again though - it’s solely dependent on your use case - there is no right or wrong way.
OK, thanks for the advise. I am going to explore bundling the initial realms in to the app and determine how I can manage those over time.