MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Swift Date initialisation problem in Nested Object

This is my schema:

    @objc class RealmTask: Object {
    @objc dynamic var _id: ObjectId? = ObjectId.generate()
    @objc dynamic var _instance: ObjectId? 
    @objc dynamic var assigneeId: ObjectId?
    @objc dynamic var comment: String?
    @objc dynamic var completeDate: RealmDateTimeOffset? 
    let completerVisitId = RealmOptional<Int>()
    @objc dynamic var createdAt: RealmDateTimeOffset?
    let done = RealmOptional<Bool>()
    @objc dynamic var dueDate: RealmDateTimeOffset?
    @objc dynamic var editorId: ObjectId?
    @objc dynamic var endTime: RealmDateTimeOffset?
    let insightsCustomerId = RealmOptional<Int>()
    let insightsId = RealmOptional<Int>()
    let isAnytime = RealmOptional<Bool>()
    @objc dynamic var reminder: RealmDateTimeOffset?
    @objc dynamic var type: String = ""
    @objc dynamic var updatedAt: RealmDateTimeOffset?
    @objc dynamic var updatedBy: ObjectId = ObjectId.generate()
    
    override static func primaryKey() -> String? {
        return "_id"
    }
}

@objc class RealmDateTimeOffset: EmbeddedObject {
    @objc dynamic var dateTime: Date = Date()
    @objc dynamic var offset: Int = 0
}

When I execute this code:

   try! realm.write {
                    let task = RealmTask()
                    task.dueDate = RealmDateTimeOffset()
                    realm.add(task)
                }

I get this error:

2020-11-18 09:14:50.739130+0200 Skynamo[66587:1209146] RLMException
2020-11-18 09:14:50.739312+0200 Skynamo[66587:1209146] Invalid value ‘2020-11-18 07:14:50 +0000’ to initialize object of type ‘RealmDateTimeOffset’: missing key ‘dateTime’
2020-11-18 09:14:50.739514+0200 Skynamo[66587:1209146] {
RLMRealmCoreVersion = “”;
RLMRealmVersion = “10.1.3”;
}

This is on Xcode 11.7

I copy and pasted your exact code and ran the app.

Everything worked as expected; a RealmTask object was created and the dateTime property was correctly populated with the embedded RealmDateTimeOffset.

macOS 10.14.6 and 10.15.x
XCode 11 & 12
Realm version: 10.1.4

Thanks for the reply.

For anyone who run into a similar issue. I had my own getters and setters that probably overrides the Realm getters and setters. Still working on it but removing them seems to solve the problem.