3

I'm experiencing an issue with CoolStorage where a OneToMany relationship is being cached when reading a list for the first time, that list then fails to update when the data changes later.

When restarting my app the list is correct, so it must be a caching issue and I can't find the solution in the CoolStorage documentation or anywhere.

I've pasted my Unit test code below.

I have a Game class that has an Opponent of type Person. There are many Games, and a Person can be an Opponent for many Games.

My Person class has a CSList called OpponentGames, so at anytime for that Person I can say 'get the games where this person is the Opponent'.

In the unit test the first time I reference 'person.OpponentGames.Count', it correctly tells me there is one game. After creating a second game and adding my person as the opponent, it still reports one game for OpponentGames.

If I remove the first count, the test passes, so the first check result is being cached and from there it's not updated.

I'll greatly appreciate any suggestions!! Thanks.

The SQL (sqlite3) setup:

CSDatabase.ExecuteNonQuery(
    "CREATE TABLE Person " +
    "(ServerId INTEGER PRIMARY KEY)");

CSDatabase.ExecuteNonQuery(
    "CREATE TABLE Game " +
    "(ServerId INTEGER PRIMARY KEY," +
    "OpponentId INTEGER REFERENCES User(ServerId))");

The CoolStorage .Net classes:

[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"); } }
}

And finally, the Unit test:

// 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);
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
danfordham
  • 980
  • 9
  • 15

0 Answers0