初めてリストを読み取るときに OneToMany 関係がキャッシュされ、後でデータが変更されたときにそのリストが更新されないという CoolStorage の問題が発生しています。
アプリを再起動すると、リストが正しいため、キャッシュの問題である必要があり、CoolStorage のドキュメントやどこにも解決策が見つかりません。
以下に単体テストコードを貼り付けました。
タイプ Person の Opponent を持つ Game クラスがあります。多くのゲームがあり、人物は多くのゲームの対戦相手になることができます。
私の Person クラスにはCSList
OpponentGames という名前があるので、いつでもその Person に対して「この人が対戦相手であるゲームを取得する」と言うことができます。
初めて「person.OpponentGames.Count」を参照する単体テストでは、ゲームが 1 つあることが正しくわかります。2 番目のゲームを作成し、対戦相手として自分の人を追加した後も、OpponentGames の 1 つのゲームが報告されます。
最初のカウントを削除すると、テストに合格するため、最初のチェック結果がキャッシュされ、そこから更新されません。
どんな提案でも大歓迎です!! ありがとう。
SQL (sqlite3)
セットアップ:
CSDatabase.ExecuteNonQuery(
"CREATE TABLE Person " +
"(ServerId INTEGER PRIMARY KEY)");
CSDatabase.ExecuteNonQuery(
"CREATE TABLE Game " +
"(ServerId INTEGER PRIMARY KEY," +
"OpponentId INTEGER REFERENCES User(ServerId))");
CoolStorage .Net クラス:
[MapTo("Game")]
public class Game : CSObject<Game,int>
{
//[PrimaryKey]
public int ServerId { get { return (int) GetField("ServerId"); } set { SetField("ServerId", value); } }
[ManyToOne (LocalKey="OpponentId", ForeignKey="ServerId")]
public Person Opponent { get { return (Person) GetField("Opponent"); } set { SetField("Opponent", value); } }
}
[MapTo("Person")]
public class Person : CSObject<Person,int>
{
//[PrimaryKey]
public int ServerId { get { return (int) GetField("ServerId"); } set { SetField("ServerId", value); } }
[OneToMany (LocalKey="ServerId", ForeignKey="OpponentId")]
public CSList<Game> OpponentGames { get { return (CSList<Game>) GetField("OpponentGames"); } }
}
そして最後に、単体テスト:
// user
Person person = Person.New ();
person.ServerId = 1;
// first game
Game game = Game.New ();
game.ServerId = 1;
game.Opponent = person;
game.Save ();
person.Save ();
// this line correctly returns 1, removing this passes the test
Console.WriteLine ("count0: " + person.OpponentGames.Count);
// second game
Game game2 = Game.New ();
game2.ServerId = 2;
game2.Opponent = person;
game2.Save ();
person.Save ();
// this incorrectly shows 1, should be 2
Console.WriteLine ("count1: " + person.OpponentGames.Count);
// and it fails
Assert.True (person.OpponentGames.Count == 2);