Reputation:
I copy and paste the code from realm doc. But don't know how to change teh section i commented next to to indicate. (at the bottom) Below is the full error message I get:
error initializing newrealm, Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'Item.dateCreated' has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'Item.dateCreated' has been added., Error Code=10} 2018-07-30 21:25:24.231575-0400 Todoey[87561:3063712] *** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid property name 'dateCreated' for class 'Category'.'
Below is the code in witch I attempted the migration:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
print(Realm.Configuration.defaultConfiguration.fileURL)
do {
let realm = try Realm()
} catch {
print("error initializing newrealm, \(error)")
}
//Migration
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.enumerateObjects(ofType: Category.className()) { (old, new) in
new!["dateCreated"] = Date()
}
migration.enumerateObjects(ofType: Item.className()) { (old, new) in
new!["dateCreated"] = Date()
}
}
})
Realm.Configuration.defaultConfiguration = config
//Migration X
return true
}
It appears that the problem is above where the "// combine name fields into a single field" comment is. I need to change those values to the following:
class Item: Object {
@objc dynamic var title: String = ""
@objc dynamic var done: Bool = false
@objc dynamic var dateCreated = NSDate() //this is the new data
var parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}
Upvotes: 6
Views: 5282
Reputation: 670
The problem is the new property which has been added to realm database.
When you run your app in simulator, it will pull up old realm database without this new property.
Solution working in Xcode 10, Swift 4.2:
NOTE: This solution is good when app is still in development and using simulators. If your app is already published and in production, then u should increment schema version of database. There is guide on Realm website how to do that.
Hope this helps.
Upvotes: 7
Reputation: 741
In your case - since you merely added a new property - all you need to do is increment the schema version to 2, and Realm will take care of the rest.
Upvotes: 0