2017-08-19 2 views
0

入れ子モデルの非常に単純なプロジェクトを作成しました。User-> Coach-> Lesson パスUser.CurrentCoach.CurrentLessonからレッスンインスタンスを取得しようとすると、常にレッスンクラスの新しいインスタンスが取得されます。例えばRealm(Net Xamarin)は常に新しいクラスインスタンスを返しますか?

:obj2のGUIDとして

var obj1 = App.CurrentUser.CurrentCoach.CurrentLesson; 
App.Realm.Write(() => App.CurrentUser.CurrentCoach.CurrentLesson.Name = "Second"); 
var obj2 = App.CurrentUser.CurrentCoach.CurrentLesson; 

ここでobj1がGUID同じではありません。 レルムは常に新しいインスタンスを返しますか?そしてこれを防ぐ方法は?

モデル:

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

    public Coach CurrentCoach { get; set; } 
} 

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

    public Lesson CurrentLesson { get; set; } 
} 

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

    public Guid guid; 
    public Lesson() 
    { 
     guid = Guid.NewGuid(); 
    } 
} 
+0

'App.CurrentUser.CurrentCoach.CurrentLesson'の後ろにあるコードは何ですか? – SushiHangover

答えて

1

あなたがプロパティにアクセスするたびに、新しいインスタンスを取得します。この動作は仕様であり、回避する方法はありません。経験則として、モデルに保持される非永続プロパティ/フィールドに頼るべきではありません。プロパティにアクセスするたびに​​フィールドが変更されないようにするには、そのフィールドをデータベースに保持する必要があります。

関連する問題