realm / realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs
https://realm.io
Apache License 2.0
1.24k stars 162 forks source link

[Bug]: Frozen object has incorrect property indices #2670

Closed nirinchev closed 2 years ago

nirinchev commented 3 years ago

Reported on the forums: https://www.mongodb.com/community/forums/t/realm-sync-net-freezing-an-object-on-a-synced-realm-messes-the-properties-up/126797

Looks like we're somehow using an incorrect object schema when looking up the properties, but it's not obvious what's causing it.

JamieColclough commented 3 years ago

I couldn't attach the zip so here's a link to the project that has it. https://github.com/JamieColclough/Frozen-object-has-incorrect-property-indices/tree/main/RealmSyncFreezeError I couldn't upload the packages but hopefully you can build the project with this

papafe commented 3 years ago

@JamieColclough thanks a lot for your project! We managed to reproduce the issue and we're currently investigating it.

JamieColclough commented 3 years ago

@papafe Thanks! in the meantime, is there a workaround I can do so I can still use the frozen realm?

papafe commented 3 years ago

@JamieColclough we found the cause of the issue and we are working on a fix. The workaround for now would be to have exactly the same schema locally as you have on MongoDB Realm (including the partition key). For example, ClinicAppointment would look like this:

    [MapTo("clinic_appointment")]
    public class ClinicAppointment : RealmObject
    {
        [PrimaryKey]
        [MapTo("_id")]
        public int AppointmentId { get; set; }

        [MapTo("_partition")]
        [Required]
        public string Partition { get; set; } //This is what you are missing right now

        [MapTo("appointment_time")]
        public DateTimeOffset AppointmentTime { get; set; }

        [MapTo("clinic_schedule_id")]
        public ClinicSchedule ClinicSchedule { get; set; }

        [MapTo("clinician_id")]
        public Clinician Clinician { get; set; }

        [MapTo("is_uploading")]
        public bool IsUploading { get; set; }

        [MapTo("patient_record")]
        public PatientRecord PatientRecord { get; set; }

        [MapTo("patient_seen")]
        public bool PatientSeen { get; set; }

        [MapTo("status_id")]
        public int StatusId { get; set; }
    }

I don't know how exactly you are using frozen objects, as I suppose the project that you shared is probably a very simplified version of your use case, but I could also suggest not freezing the realm for now and just use the live objects.

nirinchev commented 2 years ago

This was fixed in 10.7.1.