Hello.
Imagine that we have a Person
object in my realm database:
public class Person : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
Then I decide to use combined FullName
property instead of the separate FirstName
and LastName
property in the Person schema.
public class Person : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}
According to the doc this change isn’t destructive and I don’t have to create a partner collection, forward and backward migration functions.
But the problem is that I would like to migrate the realm to conform to the updated Person
schema. If I were developing an application without Realm Sync, I would use the following migration function:
var config = new RealmConfiguration
{
SchemaVersion = 4,
MigrationCallback = (migration, oldSchemaVersion) =>
{
var oldPeople = migration.OldRealm.All<Person>();
var newPeople = migration.NewRealm.All<Person>();
// Migrate Person objects
for (var i = 0; i < newPeople.Count(); i++)
{
var oldPerson = oldPeople.ElementAt(i);
var newPerson = newPeople.ElementAt(i);
// Changes from version 1 to 2 (adding LastName) will occur automatically when Realm detects the change
// Migrate Person from version 2 to 3: replace FirstName and LastName with FullName
// LastName doesn't exist in version 1
if (oldSchemaVersion < 2)
{
newPerson.FullName = oldPerson.FirstName;
}
else if (oldSchemaVersion < 3)
{
newPerson.FullName = $"{oldPerson.FirstName} {oldPerson.LastName}";
}
}
}
};
var realm = Realm.GetInstance(config);
and that would be enough to extract the FullName
from older databases combining the FirstName
and LastName
properties.
So I have the following questions:
- Do I need a partner collection for that changes?
- Does the non-sync realm migration function applicable for the realm sync? And if No what I should do in that case?