Migrating existing data from a Results to List object


Hey All,

I have a Realm Swift question I have been pondering for over a week. Would appreciate any new thoughts to explore.

I have an app in the store that folks have stored data in the Realm database. I added a new model of Lists to sort the data. I did not need to migrate the code as I did not change the original model, and that that little tidbit is not described in the Realm docs anywhere I could find! Trial and error! Lol

When I upgrade the app, all the data is secure in the Realm database, but the tableView no longer displays it as I changed the variable that contains the data from a Results type to a List type. New additions to the database show up fine and are saved as expected.

So, the question is, does anyone have any ideas about how to perform a one time adding of the current data saved to Realm into the new List objects? I do have a routine that will check to see if the user has updated, that is not the problem. I just can’t get the data already saved there into the new lists.

I have been scratching my head for over a week now, and every permeation does not work. I can save empty objects, just not the data that is already there.

Any thoughts will be greatly appreciated!





This is exactly what migration good for.


Hmm. Thanks. I will have to try this code ina migration block and see what happen.



Actualy its easier than you imagined. Just catch the version changes (like from 3 to 4) and in that move all the data to the list.
Migration occurs when there is a database-version change, not actually model changes.
[Of course, the auto-migration can do a lot of things, but i still prefer my hard-coded migrations. Our users data depends on it]


Thanks for the help.

I get an error trying to move the data in the migration block because I have to access Realm to get the data and the migration has not finished. I must be missing something right in front of me! :slight_smile:

Thanks again!


How are you trying to get the “old” data? Normal query wont work.
Intead of a normal query, you need to do in a migration block:

 migrationBlock: { migration, oldSchemaVersion in
        if (oldSchemaVersion == 8) {

            migration.enumerateObjects(ofType: Object.className()) { oldObject, newObject in
                   guard let firstName = oldObject?["firstName"] as? String,
                             let middleName = oldObject?["middleName"] as? String,
                             let lastName = oldObject?["lastName"] as? String else { return }
                   newObject!["fullName"] = "\(firstName) \(middleName) \(lastName)"

Its not the best way to handle these, but its working on my case.


Thanks so much for your time and help. I really appreciate it.

Yep, that is what I do when I need to add a new object. Works for me too.

Where I am stumped right now is getting the existing data stored in the Realm file connected to a relationship in the new Lists. When I add a new submission to the database, it shows up fine as the add code also adds the reference to the List. I just can’t figure out how to make a connection to the relationships with migration. I assumed I need to access the current Realm database, and that is when I get the error.

Anyway, thanks for your time, I do appreciate it!