I am in the process of removing all @objc dynamic
annotations from Realm objects to use the more modern @Persisted
annotation.
While it's been good for a lot of the code, I am running into issues, one of which being related to the object primary key. Here's the doc. Let's take this example:
class MyObject: Object { @objc public dynamic var uuid = "" // many other properties... override public static func primaryKey() -> String? {"uuid" }}
This would become this:
class MyObject: Object { @Persisted(primaryKey: true) @objc public var uuid: String}
But running the tests outputs this error:
Error Domain=io.realm Code=10 "Migration is required due to the following errors:- Primary Key for class 'MyObject' has been removed." UserInfo={Error Name=SchemaMismatch, NSLocalizedDescription=Migration is required due to the following errors:- Primary Key for class 'MyObject' has been removed., Error Code=10}
Note 1: the @objc
annotation is required because this property is accessed from Objective-C code. I also tested this case without the @objc
annotation and the result is the same. This is possible, according to this question/answer.
Note 2: in both cases (before/after), the uuid
is initialized this way:
override public required init() { super.init() let uuid = UUID().uuidString}
Could this be because another issue in the schema, in a related object? For example, I have "translated"
@objc public private(set) dynamic var id: Int = 1 public var categoryList = List<PersistableIdentifiableObject>() override public required init() { super.init() } override public class func primaryKey() -> String? { #keyPath(MyObject.id) }
with
@Persisted(primaryKey: true) public private(set) var id: Int @Persisted public var categoryList = List<PersistableIdentifiableObject>() override public required init() { super.init() id = 1 // Yes, this is meant to be this way. }
Thank you for any help regarding this.