This is my collection how it was looking on initial development of project
public final class RLMBlackBoxWarning: Object, Codable {
@Persisted public var values: List<RLMValue>
@Persisted public var headerKey: String
@Persisted public var id: Int?
}
Later I coverted variable to id to primary key
public final class RLMBlackBoxWarning: Object, Codable {
@Persisted public var values: List<RLMValue>
@Persisted public var headerKey: String
@Persisted(primaryKey: true) public var id: Int
}
When I try to access to access realm content I was getting error saying
Non Fatal Error: generic(Error Domain=io.realm Code=10 "Migration is required due to the following errors:
- Property ‘RLMBlackBoxWarning.id’ has been made required.
- Primary Key for class ‘RLMBlackBoxWarning’ has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors:
- Property ‘RLMBlackBoxWarning.id’ has been made required.
- Primary Key for class ‘RLMBlackBoxWarning’ has been added., Error Code=10})
Properties:
{
localizedDescription = “The operation couldn\U2019t be completed. (EpocDataKit.DrugsRealmStore.StoreError error 0.)”;
}
To resolve this added migration block
Realm.Configuration(
fileURL: self.makeRxQueuesSyncedDataFileAttributes().drugRealmURL,
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 2 {
migration.enumerateObjects(ofType: RLMBlackBoxWarning.className()) { oldObject, newObject in
// swiftlint:disable force_unwrapping
newObject!["id"] = oldObject!["id"]
}
}
},
objectTypes: .drugsRLMObjectTypes
)
After that I started getting below error
Non Fatal Error: generic(Error Domain=io.realm Code=1 “Primary key property ‘class_RLMBlackBoxWarning.id’ has duplicate values after migration.” UserInfo={NSLocalizedDescription=Primary key property ‘class_RLMBlackBoxWarning.id’ has duplicate values after migration., Error Code=1})
Properties:
{
localizedDescription = “The operation couldn\U2019t be completed. (EpocDataKit.DrugsRealmStore.StoreError error 0.)”;
}
How can I handle this scenario