4

I am seeking to add another property to my Realm database scheme (pointed to by the arrow) while at the same time learn how to use the migration functionality.

class FeesPaid: Object {
    dynamic var fileNumber = ""
    dynamic var forMonth = ""
    dynamic var amount: Float = 0.0
    dynamic var balance: Float = 0.0   <-------
    dynamic var date = ""
}

I have been following the instructions at Realm.io and I have copied the code in the first block and placed it into the my "application(application:didFinishLaunchingWithOptions:)" function which is located in the "AppDelegate.swift" file.

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: 1,

   // 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 < 1) {
          // Nothing to do!
          // Realm will automatically detect new properties and removed properties
          // And will update the schema on disk automatically
      }
   }
)

// 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
let clients = try! Realm()

The statement just below this block on the Realm web site states:
"At the very minimum all we need to do is to update the version with an empty block to indicate that the schema has been upgraded (automatically) by Realm."
(This statement of nothing else needing to be done seems to be supported by the comments within the if statement, above.)

Yet when I run my app I keep getting the error:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Migration is required for object type 'FeesPaid' due to the following errors:
- Property 'balance' has been added to latest object model." UserInfo={NSLocalizedDescription=Migration is required for object type 'FeesPaid' due to the following errors:
- Property 'balance' has been added to latest object model.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 50

So what am I doing wrong?

I don't need to enumerate or pre-fill in any values in the new field. I simply need the new field in my scheme.

Eiko
  • 25,601
  • 15
  • 56
  • 71
Delete My Account
  • 745
  • 2
  • 9
  • 21
  • Have you tried setting the schemaVersion to 2? I'm guessing you ran the app after setting the schemaVersion to 1, but before adding the property on the model. Whenever I've gotten that error before, I just bump the schemaVersion and it figures out the migration. – esthepiking Dec 23 '15 at 20:37
  • I did actually. I even tried stepping the version up to 3, 4, and 5 with no success. Is there something else I am supposed to be doing? – Delete My Account Dec 23 '15 at 21:38
  • No that should be everything. If you're just testing locally you can delete the app and reinstall which will take care of the migration issue, but that's not a good solution for a production app – esthepiking Dec 23 '15 at 21:39
  • Please mark the correct answer to your question, or write a short answer on your own and select it. The title is not suited for that. – Eiko Dec 25 '15 at 17:18

1 Answers1

3

I compared your code against the examples in the docs and the sample code and it looks fine. By all accounts that should be working.

The only thing I can think of is that that Realm object may be getting called elsewhere in your app before it's been configured in your app delegate. This is possible if your app employs storyboards and you have any Realm code in a view controller viewDidLoad method as this gets loaded before your app delegate is triggered. If that's the case, it's usually quite easy to solve as all you need to do is manually set up and present your storyboard at the end of application(application:didFinishLaunchingWithOptions:)

Failing that, what esthepiking said is correct. You can increment the schema version during testing, or if you know your schema changes are going to be very volatile during development, you can also simply delete it each time on app launch.

TiM
  • 15,812
  • 4
  • 51
  • 79
  • TiM & esthepiking: There was Realm code in my viewDidLoad function for the first storyboard scene. This code was related to naming the .realm file to something other than "default". I have move that code into the application(application:didFinishLaunchingWithOptions:) function just below the section of code performing the migration. After I did that I am now able to perform a migration. – Delete My Account Dec 25 '15 at 17:12