Search code examples
c#mongodbrealmmaui

Sub Document Not Saving With Realm


I have written a mobile app with dotnet MAUI. The data originally came from SQL but I've converted it all into Mongo collections and I have configured the Realm sync. My data is games and game consoles to store my fairly sizable retro game collection. The data transferred automatically from my Mongo DB to the Realm DB using the flexible sync and all is good.

My Mongo schema is as follows:

{
  "Console": {
    "ref": "#/relationship/mongodb-atlas/gamedata/ConsoleType",
    "foreignKey": "_id",
    "isList": false
  }
}
{
  "title": "Game",
  "bsonType": "object",
  "required": [
    "_id",
    "UserId",
    "Name",
    "IgdbId",
    "IgdbUrl",
    "HasGame",
    "HasCase",
    "HasManual",
    "IsCopy",
    "IsCollectorsEdition",
    "IsNonPalRegion"
  ],
  "properties": {
    "_id": {
      "bsonType": "uuid"
    },
    "UserId": {
      "bsonType": "uuid"
    },
    "Name": {
      "bsonType": "string"
    },
    "IgdbId": {
      "bsonType": "int"
    },
    "IgdbUrl": {
      "bsonType": "string"
    },
    "HasGame": {
      "bsonType": "bool"
    },
    "HasCase": {
      "bsonType": "bool"
    },
    "HasManual": {
      "bsonType": "bool"
    },
    "IsCopy": {
      "bsonType": "bool"
    },
    "IsCollectorsEdition": {
      "bsonType": "bool"
    },
    "IsNonPalRegion": {
      "bsonType": "bool"
    },
    "Summary": {
      "bsonType": "string"
    },
    "GameCoverUrl": {
      "bsonType": "string"
    },
    "LastUpdated": {
      "bsonType": "date"
    },
    "Notes": {
      "bsonType": "string"
    },
    "Console": {
      "bsonType": "uuid"
    },
    "OriginalId": {
      "bsonType": "long"
    }
  }
}

My issue comes when I am creating a new record - the field which has a relationship (Console) shows as null in the realm but when I step through (demo code below) this shows the console property with a valid value. I've looked at the Mongo logs (on the off chance) but there is no issue here.

 RealmService.Realm.Write(() => {
DataSource.Id = Guid.NewGuid();
var savedGame = RealmService.Realm.Add(DataSource, true);
}

I'm using the Realm nuget package version 10.18.0.

My realm models are as follows:

 public class ConsoleType : RealmObject
    {

        [PrimaryKey]
        [MapTo("_id")]
        public Guid Id { get; set; }

        public string Name { get; set; }

        public string Manufacturer { get; set; }   

        public string ImageUrl { get; set; }

        public int IgdbId { get; set; }

        public DateTimeOffset? IgdbLastUpdated { get; set; }

        public string Checksum { get; set; }

        public string Slug { get; set; }

        public string AlternativeName { get; set; }

        public DateTimeOffset? LastUpdated { get; set; }

        public bool Enabled { get; set; }

        public int? OriginalId { get; set; }

    }
public class Game : RealmObject
    {

        [PrimaryKey]
        [MapTo("_id")]
        public Guid Id { get; set; }

        [Required]
        public string Name { get; set; }

        public int IgdbId { get; set; }

        [Required]
        public string IgdbUrl { get; set; }

        public bool HasGame { get; set; }

        public bool HasCase { get; set; }

        public bool HasManual { get; set; }

        public bool IsCopy { get; set; }

        public bool IsCollectorsEdition { get; set; }

        public bool IsNonPalRegion { get; set; }

        public string? Summary { get; set; } 

        public string? GameCoverUrl { get; set; }

        [Indexed]
        public Guid UserId { get; set; }


        public DateTimeOffset? LastUpdated { get; set; }

        public string? Notes { get; set; }

        public ConsoleType Console { get; set; }

        public int? OriginalId { get; set; }

    }

My flow here is that I have a basic UI

UI

Click save and if I set a breakpoint I can see the console as part of the model to save.

Breakpoint

Really struggling to understand where I'm going wrong and looking for some assistance.


Solution

  • Thanks for coming back to me on this Andrea but I've managed to fix my issue.

    I went back and re-read the documentation - my use case should have worked but then again after looking at what I wanted to achieve I realised that an embedded document was actually the way to go. I didn't need all of the fields from the console record to be moved across I simply needed a couple (_id and name).

    I wiped my MongoDb collections and started again with the data being structured more efficiently and then created an embedded console inheriting from a EmbeddedObject instead of a RealmObject - not sure if that was the problem or not.

    However - this issue aside - working with data has become much simpler using Realm so I've decided to migrate another project of mine across in the next month or so. If I get any issues here, I'll definitely log it through github.

    Thanks again!