Getting invalid property name when trying to perform Realm migration









up vote
0
down vote

favorite












I'm having trouble with Realm giving me the error that a property of a given name does not exist for my object. But I know it does exist.



I've tried to follow the docs at https://realm.io/docs/swift/latest/#updating-values. I've searched for everything I can think of to find an applicable solution here and elsewhere, but nothing I've found works.



I previously performed a simple migration to just add properties to a different object within the same Realm. I just left the migration block empty and that worked fine. That migration should have set my schema from 0 to 1. If I run this with my schema set to 1 and to check for versions less than 2, it tells me that migration must be run in order to add these properties. The migration is running. I have a print statement for every time it executes. If I set the schema to 2, still checking for less than 2, I get the error for invalid property name for the old properties unless I uncomment them. Then I still get the error for the new properties.



Here's my Realm object. The commented out lines are the old properties I want to migrate from. The Int values are what I'm migrating to.



@objcMembers class Options: Object 

// dynamic var morningStartTime: Date?
// dynamic var afternoonStartTime: Date?
// dynamic var eveningStartTime: Date?
// dynamic var nightStartTime: Date?

dynamic var morningHour: Int = 7
dynamic var morningMinute: Int = 0

dynamic var afternoonHour: Int = 12
dynamic var afternoonMinute: Int = 0

dynamic var eveningHour: Int = 17
dynamic var eveningMinute: Int = 0

dynamic var nightHour: Int = 21
dynamic var nightMinute: Int = 0

dynamic var morningNotificationsOn: Bool = true
dynamic var afternoonNotificationsOn: Bool = true
dynamic var eveningNotificationsOn: Bool = true
dynamic var nightNotificationsOn: Bool = true

dynamic var firstItemAdded: Bool = false

dynamic var smartSnooze: Bool = false

dynamic var optionsKey = UUID().uuidString
override static func primaryKey() -> String?
return "optionsKey"





My migration block:



 let config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 2,

// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0

if (oldSchemaVersion < 2)

migration.enumerateObjects(ofType: Options.className(), (newObject, oldObject) in
let morningStartTime = oldObject!["morningStartTime"] as! Date?
let afternoonStartTime = oldObject!["afternoonStartTime"] as! Date?
let eveningStartTime = oldObject!["eveningStartTime"] as! Date?
let nightStartTime = oldObject!["nightStartTime"] as! Date?

newObject!["morningHour"] = self.getHour(date: morningStartTime)
newObject!["morningMinute"] = self.getMinute(date: morningStartTime)

newObject!["afternoonHour"] = self.getHour(date: afternoonStartTime)
newObject!["afternoonMinute"] = self.getMinute(date: afternoonStartTime)

newObject!["eveningHour"] = self.getHour(date: eveningStartTime)
newObject!["eveningMinute"] = self.getMinute(date: eveningStartTime)

newObject!["nightHour"] = self.getHour(date: nightStartTime)
newObject!["nightMinute"] = self.getMinute(date: nightStartTime)
)

)


getHour and getMinute are just functions I wrote to return an Int for the hour or minute from a Date. In case it's relevant, here they are.



func getHour(date: Date?) -> Int 
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH"
let hour = dateFormatter.string(from: date!)
return Int(hour)!


func getMinute(date: Date?) -> Int
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "mm"
let minutes = dateFormatter.string(from: date!)
return Int(minutes)!










share|improve this question



























    up vote
    0
    down vote

    favorite












    I'm having trouble with Realm giving me the error that a property of a given name does not exist for my object. But I know it does exist.



    I've tried to follow the docs at https://realm.io/docs/swift/latest/#updating-values. I've searched for everything I can think of to find an applicable solution here and elsewhere, but nothing I've found works.



    I previously performed a simple migration to just add properties to a different object within the same Realm. I just left the migration block empty and that worked fine. That migration should have set my schema from 0 to 1. If I run this with my schema set to 1 and to check for versions less than 2, it tells me that migration must be run in order to add these properties. The migration is running. I have a print statement for every time it executes. If I set the schema to 2, still checking for less than 2, I get the error for invalid property name for the old properties unless I uncomment them. Then I still get the error for the new properties.



    Here's my Realm object. The commented out lines are the old properties I want to migrate from. The Int values are what I'm migrating to.



    @objcMembers class Options: Object 

    // dynamic var morningStartTime: Date?
    // dynamic var afternoonStartTime: Date?
    // dynamic var eveningStartTime: Date?
    // dynamic var nightStartTime: Date?

    dynamic var morningHour: Int = 7
    dynamic var morningMinute: Int = 0

    dynamic var afternoonHour: Int = 12
    dynamic var afternoonMinute: Int = 0

    dynamic var eveningHour: Int = 17
    dynamic var eveningMinute: Int = 0

    dynamic var nightHour: Int = 21
    dynamic var nightMinute: Int = 0

    dynamic var morningNotificationsOn: Bool = true
    dynamic var afternoonNotificationsOn: Bool = true
    dynamic var eveningNotificationsOn: Bool = true
    dynamic var nightNotificationsOn: Bool = true

    dynamic var firstItemAdded: Bool = false

    dynamic var smartSnooze: Bool = false

    dynamic var optionsKey = UUID().uuidString
    override static func primaryKey() -> String?
    return "optionsKey"





    My migration block:



     let config = Realm.Configuration(
    // Set the new schema version. This must be greater than the previously used
    // version (if you've never set a schema version before, the version is 0).
    schemaVersion: 2,

    // Set the block which will be called automatically when opening a Realm with
    // a schema version lower than the one set above
    migrationBlock: migration, oldSchemaVersion in
    // We haven’t migrated anything yet, so oldSchemaVersion == 0

    if (oldSchemaVersion < 2)

    migration.enumerateObjects(ofType: Options.className(), (newObject, oldObject) in
    let morningStartTime = oldObject!["morningStartTime"] as! Date?
    let afternoonStartTime = oldObject!["afternoonStartTime"] as! Date?
    let eveningStartTime = oldObject!["eveningStartTime"] as! Date?
    let nightStartTime = oldObject!["nightStartTime"] as! Date?

    newObject!["morningHour"] = self.getHour(date: morningStartTime)
    newObject!["morningMinute"] = self.getMinute(date: morningStartTime)

    newObject!["afternoonHour"] = self.getHour(date: afternoonStartTime)
    newObject!["afternoonMinute"] = self.getMinute(date: afternoonStartTime)

    newObject!["eveningHour"] = self.getHour(date: eveningStartTime)
    newObject!["eveningMinute"] = self.getMinute(date: eveningStartTime)

    newObject!["nightHour"] = self.getHour(date: nightStartTime)
    newObject!["nightMinute"] = self.getMinute(date: nightStartTime)
    )

    )


    getHour and getMinute are just functions I wrote to return an Int for the hour or minute from a Date. In case it's relevant, here they are.



    func getHour(date: Date?) -> Int 
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH"
    let hour = dateFormatter.string(from: date!)
    return Int(hour)!


    func getMinute(date: Date?) -> Int
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "mm"
    let minutes = dateFormatter.string(from: date!)
    return Int(minutes)!










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having trouble with Realm giving me the error that a property of a given name does not exist for my object. But I know it does exist.



      I've tried to follow the docs at https://realm.io/docs/swift/latest/#updating-values. I've searched for everything I can think of to find an applicable solution here and elsewhere, but nothing I've found works.



      I previously performed a simple migration to just add properties to a different object within the same Realm. I just left the migration block empty and that worked fine. That migration should have set my schema from 0 to 1. If I run this with my schema set to 1 and to check for versions less than 2, it tells me that migration must be run in order to add these properties. The migration is running. I have a print statement for every time it executes. If I set the schema to 2, still checking for less than 2, I get the error for invalid property name for the old properties unless I uncomment them. Then I still get the error for the new properties.



      Here's my Realm object. The commented out lines are the old properties I want to migrate from. The Int values are what I'm migrating to.



      @objcMembers class Options: Object 

      // dynamic var morningStartTime: Date?
      // dynamic var afternoonStartTime: Date?
      // dynamic var eveningStartTime: Date?
      // dynamic var nightStartTime: Date?

      dynamic var morningHour: Int = 7
      dynamic var morningMinute: Int = 0

      dynamic var afternoonHour: Int = 12
      dynamic var afternoonMinute: Int = 0

      dynamic var eveningHour: Int = 17
      dynamic var eveningMinute: Int = 0

      dynamic var nightHour: Int = 21
      dynamic var nightMinute: Int = 0

      dynamic var morningNotificationsOn: Bool = true
      dynamic var afternoonNotificationsOn: Bool = true
      dynamic var eveningNotificationsOn: Bool = true
      dynamic var nightNotificationsOn: Bool = true

      dynamic var firstItemAdded: Bool = false

      dynamic var smartSnooze: Bool = false

      dynamic var optionsKey = UUID().uuidString
      override static func primaryKey() -> String?
      return "optionsKey"





      My migration block:



       let config = Realm.Configuration(
      // Set the new schema version. This must be greater than the previously used
      // version (if you've never set a schema version before, the version is 0).
      schemaVersion: 2,

      // Set the block which will be called automatically when opening a Realm with
      // a schema version lower than the one set above
      migrationBlock: migration, oldSchemaVersion in
      // We haven’t migrated anything yet, so oldSchemaVersion == 0

      if (oldSchemaVersion < 2)

      migration.enumerateObjects(ofType: Options.className(), (newObject, oldObject) in
      let morningStartTime = oldObject!["morningStartTime"] as! Date?
      let afternoonStartTime = oldObject!["afternoonStartTime"] as! Date?
      let eveningStartTime = oldObject!["eveningStartTime"] as! Date?
      let nightStartTime = oldObject!["nightStartTime"] as! Date?

      newObject!["morningHour"] = self.getHour(date: morningStartTime)
      newObject!["morningMinute"] = self.getMinute(date: morningStartTime)

      newObject!["afternoonHour"] = self.getHour(date: afternoonStartTime)
      newObject!["afternoonMinute"] = self.getMinute(date: afternoonStartTime)

      newObject!["eveningHour"] = self.getHour(date: eveningStartTime)
      newObject!["eveningMinute"] = self.getMinute(date: eveningStartTime)

      newObject!["nightHour"] = self.getHour(date: nightStartTime)
      newObject!["nightMinute"] = self.getMinute(date: nightStartTime)
      )

      )


      getHour and getMinute are just functions I wrote to return an Int for the hour or minute from a Date. In case it's relevant, here they are.



      func getHour(date: Date?) -> Int 
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "HH"
      let hour = dateFormatter.string(from: date!)
      return Int(hour)!


      func getMinute(date: Date?) -> Int
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "mm"
      let minutes = dateFormatter.string(from: date!)
      return Int(minutes)!










      share|improve this question















      I'm having trouble with Realm giving me the error that a property of a given name does not exist for my object. But I know it does exist.



      I've tried to follow the docs at https://realm.io/docs/swift/latest/#updating-values. I've searched for everything I can think of to find an applicable solution here and elsewhere, but nothing I've found works.



      I previously performed a simple migration to just add properties to a different object within the same Realm. I just left the migration block empty and that worked fine. That migration should have set my schema from 0 to 1. If I run this with my schema set to 1 and to check for versions less than 2, it tells me that migration must be run in order to add these properties. The migration is running. I have a print statement for every time it executes. If I set the schema to 2, still checking for less than 2, I get the error for invalid property name for the old properties unless I uncomment them. Then I still get the error for the new properties.



      Here's my Realm object. The commented out lines are the old properties I want to migrate from. The Int values are what I'm migrating to.



      @objcMembers class Options: Object 

      // dynamic var morningStartTime: Date?
      // dynamic var afternoonStartTime: Date?
      // dynamic var eveningStartTime: Date?
      // dynamic var nightStartTime: Date?

      dynamic var morningHour: Int = 7
      dynamic var morningMinute: Int = 0

      dynamic var afternoonHour: Int = 12
      dynamic var afternoonMinute: Int = 0

      dynamic var eveningHour: Int = 17
      dynamic var eveningMinute: Int = 0

      dynamic var nightHour: Int = 21
      dynamic var nightMinute: Int = 0

      dynamic var morningNotificationsOn: Bool = true
      dynamic var afternoonNotificationsOn: Bool = true
      dynamic var eveningNotificationsOn: Bool = true
      dynamic var nightNotificationsOn: Bool = true

      dynamic var firstItemAdded: Bool = false

      dynamic var smartSnooze: Bool = false

      dynamic var optionsKey = UUID().uuidString
      override static func primaryKey() -> String?
      return "optionsKey"





      My migration block:



       let config = Realm.Configuration(
      // Set the new schema version. This must be greater than the previously used
      // version (if you've never set a schema version before, the version is 0).
      schemaVersion: 2,

      // Set the block which will be called automatically when opening a Realm with
      // a schema version lower than the one set above
      migrationBlock: migration, oldSchemaVersion in
      // We haven’t migrated anything yet, so oldSchemaVersion == 0

      if (oldSchemaVersion < 2)

      migration.enumerateObjects(ofType: Options.className(), (newObject, oldObject) in
      let morningStartTime = oldObject!["morningStartTime"] as! Date?
      let afternoonStartTime = oldObject!["afternoonStartTime"] as! Date?
      let eveningStartTime = oldObject!["eveningStartTime"] as! Date?
      let nightStartTime = oldObject!["nightStartTime"] as! Date?

      newObject!["morningHour"] = self.getHour(date: morningStartTime)
      newObject!["morningMinute"] = self.getMinute(date: morningStartTime)

      newObject!["afternoonHour"] = self.getHour(date: afternoonStartTime)
      newObject!["afternoonMinute"] = self.getMinute(date: afternoonStartTime)

      newObject!["eveningHour"] = self.getHour(date: eveningStartTime)
      newObject!["eveningMinute"] = self.getMinute(date: eveningStartTime)

      newObject!["nightHour"] = self.getHour(date: nightStartTime)
      newObject!["nightMinute"] = self.getMinute(date: nightStartTime)
      )

      )


      getHour and getMinute are just functions I wrote to return an Int for the hour or minute from a Date. In case it's relevant, here they are.



      func getHour(date: Date?) -> Int 
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "HH"
      let hour = dateFormatter.string(from: date!)
      return Int(hour)!


      func getMinute(date: Date?) -> Int
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "mm"
      let minutes = dateFormatter.string(from: date!)
      return Int(minutes)!







      swift realm realm-migration






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 7:59









      Hong Ooi

      41.8k1090133




      41.8k1090133










      asked Nov 10 at 7:45









      Donavon

      11




      11






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I know this is not the way to do it, but I made it work by taking a slightly more manual approach to the migration block. I uncommented the old properties in the Options object and changed my migration function to the following:



          func migrateRealm() 

          let configCheck = Realm.Configuration();
          do
          let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
          print("schema version (fileUrlIs)")
          catch
          print(error)


          print("performing realm migration")
          let config = Realm.Configuration(
          // Set the new schema version. This must be greater than the previously used
          // version (if you've never set a schema version before, the version is 0).
          schemaVersion: 2,

          // Set the block which will be called automatically when opening a Realm with
          // a schema version lower than the one set above
          migrationBlock: migration, oldSchemaVersion in
          print("oldSchemaVersion: (oldSchemaVersion)")
          if (oldSchemaVersion < 2)
          print("Migration block running")
          DispatchQueue(label: self.realmDispatchQueueLabel).async
          autoreleasepool
          let realm = try! Realm()
          let options = realm.object(ofType: Options.self, forPrimaryKey: self.optionsKey)

          do
          try realm.write
          if let morningTime = options?.morningStartTime
          options?.morningHour = self.getHour(date: morningTime)
          options?.morningMinute = self.getMinute(date: morningTime)

          if let afternoonTime = options?.afternoonStartTime
          options?.afternoonHour = self.getHour(date: afternoonTime)
          options?.afternoonMinute = self.getMinute(date: afternoonTime)

          if let eveningTime = options?.eveningStartTime
          options?.eveningHour = self.getHour(date: eveningTime)
          options?.eveningMinute = self.getMinute(date: eveningTime)

          if let nightTime = options?.nightStartTime
          options?.nightHour = self.getHour(date: nightTime)
          options?.nightMinute = self.getMinute(date: nightTime)


          catch
          print("Error with migration")





          )

          // Tell Realm to use this new configuration object for the default Realm
          Realm.Configuration.defaultConfiguration = config

          // Now that we've told Realm how to handle the schema change, opening the file
          // will automatically perform the migration
          _ = try! Realm()



          This only worked if I queued it on another thread asynchronously. I imagine if this data had been necessary for my initial view controller, then it probably would have created a race condition that caused the app to crash. Luckily this only appears in a secondary view, so it had time to complete before these values are needed. I guess I'll have to remove the unused properties with an updated Realm schema in a future version of my app.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237005%2fgetting-invalid-property-name-when-trying-to-perform-realm-migration%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I know this is not the way to do it, but I made it work by taking a slightly more manual approach to the migration block. I uncommented the old properties in the Options object and changed my migration function to the following:



            func migrateRealm() 

            let configCheck = Realm.Configuration();
            do
            let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
            print("schema version (fileUrlIs)")
            catch
            print(error)


            print("performing realm migration")
            let config = Realm.Configuration(
            // Set the new schema version. This must be greater than the previously used
            // version (if you've never set a schema version before, the version is 0).
            schemaVersion: 2,

            // Set the block which will be called automatically when opening a Realm with
            // a schema version lower than the one set above
            migrationBlock: migration, oldSchemaVersion in
            print("oldSchemaVersion: (oldSchemaVersion)")
            if (oldSchemaVersion < 2)
            print("Migration block running")
            DispatchQueue(label: self.realmDispatchQueueLabel).async
            autoreleasepool
            let realm = try! Realm()
            let options = realm.object(ofType: Options.self, forPrimaryKey: self.optionsKey)

            do
            try realm.write
            if let morningTime = options?.morningStartTime
            options?.morningHour = self.getHour(date: morningTime)
            options?.morningMinute = self.getMinute(date: morningTime)

            if let afternoonTime = options?.afternoonStartTime
            options?.afternoonHour = self.getHour(date: afternoonTime)
            options?.afternoonMinute = self.getMinute(date: afternoonTime)

            if let eveningTime = options?.eveningStartTime
            options?.eveningHour = self.getHour(date: eveningTime)
            options?.eveningMinute = self.getMinute(date: eveningTime)

            if let nightTime = options?.nightStartTime
            options?.nightHour = self.getHour(date: nightTime)
            options?.nightMinute = self.getMinute(date: nightTime)


            catch
            print("Error with migration")





            )

            // Tell Realm to use this new configuration object for the default Realm
            Realm.Configuration.defaultConfiguration = config

            // Now that we've told Realm how to handle the schema change, opening the file
            // will automatically perform the migration
            _ = try! Realm()



            This only worked if I queued it on another thread asynchronously. I imagine if this data had been necessary for my initial view controller, then it probably would have created a race condition that caused the app to crash. Luckily this only appears in a secondary view, so it had time to complete before these values are needed. I guess I'll have to remove the unused properties with an updated Realm schema in a future version of my app.






            share|improve this answer
























              up vote
              0
              down vote













              I know this is not the way to do it, but I made it work by taking a slightly more manual approach to the migration block. I uncommented the old properties in the Options object and changed my migration function to the following:



              func migrateRealm() 

              let configCheck = Realm.Configuration();
              do
              let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
              print("schema version (fileUrlIs)")
              catch
              print(error)


              print("performing realm migration")
              let config = Realm.Configuration(
              // Set the new schema version. This must be greater than the previously used
              // version (if you've never set a schema version before, the version is 0).
              schemaVersion: 2,

              // Set the block which will be called automatically when opening a Realm with
              // a schema version lower than the one set above
              migrationBlock: migration, oldSchemaVersion in
              print("oldSchemaVersion: (oldSchemaVersion)")
              if (oldSchemaVersion < 2)
              print("Migration block running")
              DispatchQueue(label: self.realmDispatchQueueLabel).async
              autoreleasepool
              let realm = try! Realm()
              let options = realm.object(ofType: Options.self, forPrimaryKey: self.optionsKey)

              do
              try realm.write
              if let morningTime = options?.morningStartTime
              options?.morningHour = self.getHour(date: morningTime)
              options?.morningMinute = self.getMinute(date: morningTime)

              if let afternoonTime = options?.afternoonStartTime
              options?.afternoonHour = self.getHour(date: afternoonTime)
              options?.afternoonMinute = self.getMinute(date: afternoonTime)

              if let eveningTime = options?.eveningStartTime
              options?.eveningHour = self.getHour(date: eveningTime)
              options?.eveningMinute = self.getMinute(date: eveningTime)

              if let nightTime = options?.nightStartTime
              options?.nightHour = self.getHour(date: nightTime)
              options?.nightMinute = self.getMinute(date: nightTime)


              catch
              print("Error with migration")





              )

              // Tell Realm to use this new configuration object for the default Realm
              Realm.Configuration.defaultConfiguration = config

              // Now that we've told Realm how to handle the schema change, opening the file
              // will automatically perform the migration
              _ = try! Realm()



              This only worked if I queued it on another thread asynchronously. I imagine if this data had been necessary for my initial view controller, then it probably would have created a race condition that caused the app to crash. Luckily this only appears in a secondary view, so it had time to complete before these values are needed. I guess I'll have to remove the unused properties with an updated Realm schema in a future version of my app.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                I know this is not the way to do it, but I made it work by taking a slightly more manual approach to the migration block. I uncommented the old properties in the Options object and changed my migration function to the following:



                func migrateRealm() 

                let configCheck = Realm.Configuration();
                do
                let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
                print("schema version (fileUrlIs)")
                catch
                print(error)


                print("performing realm migration")
                let config = Realm.Configuration(
                // Set the new schema version. This must be greater than the previously used
                // version (if you've never set a schema version before, the version is 0).
                schemaVersion: 2,

                // Set the block which will be called automatically when opening a Realm with
                // a schema version lower than the one set above
                migrationBlock: migration, oldSchemaVersion in
                print("oldSchemaVersion: (oldSchemaVersion)")
                if (oldSchemaVersion < 2)
                print("Migration block running")
                DispatchQueue(label: self.realmDispatchQueueLabel).async
                autoreleasepool
                let realm = try! Realm()
                let options = realm.object(ofType: Options.self, forPrimaryKey: self.optionsKey)

                do
                try realm.write
                if let morningTime = options?.morningStartTime
                options?.morningHour = self.getHour(date: morningTime)
                options?.morningMinute = self.getMinute(date: morningTime)

                if let afternoonTime = options?.afternoonStartTime
                options?.afternoonHour = self.getHour(date: afternoonTime)
                options?.afternoonMinute = self.getMinute(date: afternoonTime)

                if let eveningTime = options?.eveningStartTime
                options?.eveningHour = self.getHour(date: eveningTime)
                options?.eveningMinute = self.getMinute(date: eveningTime)

                if let nightTime = options?.nightStartTime
                options?.nightHour = self.getHour(date: nightTime)
                options?.nightMinute = self.getMinute(date: nightTime)


                catch
                print("Error with migration")





                )

                // Tell Realm to use this new configuration object for the default Realm
                Realm.Configuration.defaultConfiguration = config

                // Now that we've told Realm how to handle the schema change, opening the file
                // will automatically perform the migration
                _ = try! Realm()



                This only worked if I queued it on another thread asynchronously. I imagine if this data had been necessary for my initial view controller, then it probably would have created a race condition that caused the app to crash. Luckily this only appears in a secondary view, so it had time to complete before these values are needed. I guess I'll have to remove the unused properties with an updated Realm schema in a future version of my app.






                share|improve this answer












                I know this is not the way to do it, but I made it work by taking a slightly more manual approach to the migration block. I uncommented the old properties in the Options object and changed my migration function to the following:



                func migrateRealm() 

                let configCheck = Realm.Configuration();
                do
                let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
                print("schema version (fileUrlIs)")
                catch
                print(error)


                print("performing realm migration")
                let config = Realm.Configuration(
                // Set the new schema version. This must be greater than the previously used
                // version (if you've never set a schema version before, the version is 0).
                schemaVersion: 2,

                // Set the block which will be called automatically when opening a Realm with
                // a schema version lower than the one set above
                migrationBlock: migration, oldSchemaVersion in
                print("oldSchemaVersion: (oldSchemaVersion)")
                if (oldSchemaVersion < 2)
                print("Migration block running")
                DispatchQueue(label: self.realmDispatchQueueLabel).async
                autoreleasepool
                let realm = try! Realm()
                let options = realm.object(ofType: Options.self, forPrimaryKey: self.optionsKey)

                do
                try realm.write
                if let morningTime = options?.morningStartTime
                options?.morningHour = self.getHour(date: morningTime)
                options?.morningMinute = self.getMinute(date: morningTime)

                if let afternoonTime = options?.afternoonStartTime
                options?.afternoonHour = self.getHour(date: afternoonTime)
                options?.afternoonMinute = self.getMinute(date: afternoonTime)

                if let eveningTime = options?.eveningStartTime
                options?.eveningHour = self.getHour(date: eveningTime)
                options?.eveningMinute = self.getMinute(date: eveningTime)

                if let nightTime = options?.nightStartTime
                options?.nightHour = self.getHour(date: nightTime)
                options?.nightMinute = self.getMinute(date: nightTime)


                catch
                print("Error with migration")





                )

                // Tell Realm to use this new configuration object for the default Realm
                Realm.Configuration.defaultConfiguration = config

                // Now that we've told Realm how to handle the schema change, opening the file
                // will automatically perform the migration
                _ = try! Realm()



                This only worked if I queued it on another thread asynchronously. I imagine if this data had been necessary for my initial view controller, then it probably would have created a race condition that caused the app to crash. Luckily this only appears in a secondary view, so it had time to complete before these values are needed. I guess I'll have to remove the unused properties with an updated Realm schema in a future version of my app.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 20:27









                Donavon

                11




                11



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237005%2fgetting-invalid-property-name-when-trying-to-perform-realm-migration%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo