Problem with loading nested collections


#1

I cant load nested Collections in Xamarin ListView.

Lets say I have this models:

public class Lexicon : RealmObject
{
    public string Id {get;set;}
    public DateTime CreatedOn {get;set;}
    public Owner Owner {get;set;}
    public IList<Question> Question {get;set;}
}

public class Question : RealmObject
{
    public string Id {get;set;}
    public string Description {get;set;}
    public IList<Option> Options {get;set;}
}

public class Option : RealmObject
{
    public string Id {get;set;}
    public string Name {get;set;}    
}

I am trying to show all my data in ListView. I have a ViewModel that gets a list of all questions for current lexicon and then list all the questions. Every Question has many Options, but when I select the picker, options list is empty. Bindings are fine, I’ve already tested them.

Is there any way to explicitly load all nested objects like Include in EF?


#2

Realm will load everything on demand, so there’s no need to force load things. Have you verified that you do have the values persisted in the Realm? Looking at the models, it seems they are incorrect and I’m not sure if that’s due to copy-pasting or are they legitimately wrong in your app as well. The IList<T> properties should not define setters - they should be getter only properties as the IList implementation will be provided by Realm when accessing the property. So your model should look like:

public class Lexicon : RealmObject
{
    // Other properties

    public IList<Question> Question { get; } // <-- no {set;}
}

#3

Yes it was a copy-paste mistake in the app there is no setters. Actually when I write setter Visual Studio throws an error, and yes all the values are persisting in Realm I can see them in Realm Studio and when debugging. It looks like they doesn’t load by the picker and I’m trying to solve it.