How to update existent Realm List using Migrate?


#1

I have Realm database pre-bundle on my app so when user open the app First time it will moved to Livery Folder

Right now I notice there are mistakes on some recorded data on Realm List so I open The pre-bundle Realm file using Realm Browser and modified it

What I’m trying to do is , when I update my app and user open the app frist time after update it will change the records on old file which is Realm File on Livery Folder

Notice there is some Objects user can modify it , so I can’t delete old Realm database and replace it with new Realm database

So I understand I need to use Realm Migrate to make this changes

But I don’t know how to modify it

My Realm Model looks like this

public class DicModel : Object {

    public var DicListEng  = List<English>()

}


public class English : Object {
    @objc dynamic var Word : String? = nil
    @objc dynamic var Meaning : String? = nil
}
migrate Function
   static func migrate(_ migration: Migration , fileSchemaVersion: UInt64){


    if fileSchemaVersion < 2 {

        migration.enumerateObjects(ofType:  DicModel.className(), { (oldObject, newObject) in
            if let oldObject = oldObject , let newObject = newObject {
// I did try this  and got this errror : Thread 1: signal SIGABRT

  oldObject["DicListEng"] = newObject["DicListEng"] 
// Also I try this and I got error: Ambiguous use of value(forKey:) 

(oldObject["DicListEng"] as! List<MigrationObject>).value(forKey: "Meaning") = (newObject["DicListEng"] as! List<MigrationObject>).value(forKey: "Meaning")

                }
            })   
    }}

#2

@X901Take a look at these StackOverflow questions - they should help you get through setting up a migration:


#3

Thank you for your replay

I did trying the same way @ Awwadeto did on his example code

but still nothing changed in Realm file

I make the change on pre-bundle realm file
and then change the fileSchemaVersion to 2

and make the condition
fileSchemaVersion == 1 {
}

but nothing change on the Realm file on device


#4

@X901 I just took a look at this with another engineer and it looks like you are just trying to update the values of your objects - not the schema definition.

Migrations are for schema changes which is why your migration function is not executing.

What you should do on app launch is open the realm and simply update each value in the list in a realm.write transaction


#5

Thank you ,
So I was looking in wrong way

anyway

that mean I need to use userduflat with bool value as false
and then filtering result and change the vlaues

then change userdefult to true

to make the change on realm file only once time

Right ?


#6

@X901- Sure - I’d use NSUserDefaults or something like that and save a preference.