Reputation: 83
My Realm schema define a property as array of string. I add this property in my .NET model as a RealmList<string>
but this is always an empty list. I also try with an IList<string>
but I have a null
value.
Someone already try to map a list of primitive instead of a list of EmbeddedObject
or RealmObject
?
My schema:
{
"title": "simple",
"bsonType": "object",
"properties": {
"_id": {
"bsonType": "objectId"
},
"_partition": {
"bsonType": "string"
},
"name": {
"bsonType": "string"
},
"hashtags": {
"bsonType": "array",
"items": {
"bsonType": "string"
}
}
},
"required": [
"_id",
"_partition",
"name"
]
}
An example of data stored
{
"_id": {
"$oid": "6151a0eb49d3cbc1b3795579"
},
"_partition": "Part001",
"name": "First obj",
"hashtags": ["001", "0003"]
}
My .NET model:
[MapTo("simple")]
public class Simple : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[Required]
[MapTo("_partition")]
public string Partition { get; set; }
[Required]
[MapTo("name")]
public string Name { get; set; }
[MapTo("hashtags")]
public RealmList<string> Hashtags { get; }
}
.NET code that load data
var app = App.Create("my-realm");
var user = await app.LogInAsync(Credentials.EmailPassword("", ""));
var configuration = new SyncConfiguration("Part001", user)
{
ObjectClasses = new[] { typeof(Simple) }
};
var realm = await Realm.GetInstanceAsync(configuration);
await realm.GetSession().WaitForDownloadAsync();
await realm.GetSession().WaitForUploadAsync();
var simples = realm.All<Simple>().ToList();
Upvotes: 0
Views: 492
Reputation: 83
I resolved it to use IList and ISet when I use an unique constraint for array.
[MapTo("simple")]
public class Simple : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[Required]
[MapTo("_partition")]
public string Partition { get; set; }
[Required]
[MapTo("name")]
public string Name { get; set; }
[MapTo("hashtags")]
public IList<string> Hashtags { get; }
// If I use "uniqueItems": true in my schema
// "hashtags": {
// "bsonType": "array",
// "uniqueItems": true,
// "items": {
// "bsonType": "string"
// }
// }
// [Required]
// [MapTo("hashtags")]
// public ISet<string> Hashtags { get; }
}
Upvotes: 2