.NET Data loading optimization

Viewed 49

I have seen multiple threads talking about similar problems, but none of them explain my situation or explain how to solve that problem properly.

The current Problem I am facing is that I have a DataGridView, which loads in Data from a SQLite Database asynchronously, but takes too long.

The way Data is added in the SQLite Database is by downloading a JSON file, deserializing it and then adding the deserialized file as class in a Table.

Currently there are 20 rows in the Database and it consists of 8 values, e.g. ID, Name, Price..

Sounds like there is no problem, right? Now when I want to add a new player into the list, e.g.

// Note this is pseudo-code as my code is a little longer (like the Process of creating a Player)
// So I'll just TL;DR the code as I think this is the most crucial information.
public async void btnAddPlayer(){
 string website = "website.com/playerdata.json";
 // This gets user data, deserializes it, creates a player object and returns it.
 var player = GetUserDataFromJSON(website);  
 // This adds a Player asynchronously to the database
 await database.AddPlayer(player);
}

Also to note is, the JSON consists of a Table created from [UnixTime, Price]. I get the JSON by creating a HTTPWebRequest/Response and read it with a StreamReader.

(Creating WebRequest -> Getting Response -> StreamReader -> Deserializing)

Before adding a Player, the highest price is selected, so:

// This is normally no function, just put it in a function to break it down separately for this purpose
public void Function(...) {
 var highestPrice = 0;
 var highestPriceDate = DateTime.MinValue;
 foreach(var price in table.Price) {
 if(price >= highestPrice) {
 highestPrice = price;
 highestPriceDate = UnixToUTC(table.UnixTime);
}

The problem I'm facing with the add function is that adding the Player (and making it visible in the DataGridView) takes about 1 second. The way I'm visually updating is cloning row[0] and changing its Data. The result I'm awaiting is a few ms as it is just getting data from a JSON, deserializing it, (comparing prices), adding it to the DB and adding it into the DataGridView.

Now I also have a REFRESH Function (THE BIGGEST PROBLEM!), which gets newest Data for each Player available in the SQLite Table.

Therefore I first access the Database and Get all the Players.

public List<Player> GetAllPlayers(){
 // To access Table and make a query
 var query = dbConnection.Table<Player>();
 allPlayers = await query.ToListAsync();
 await conn.CloseAsync();
 return allPlayers;
}

Then I call an async Task to Update them all, which looks like this:

public async Task UpdateAllPlayers() {
 var allPlayers = await database.GetAllPlayers() {
  foreach (var player in allPlayers) {
   // Boolean "true" is marking whether it is an update.
   // If it is true, it is basically the same as btnAddPlayer(), but
   // The only difference is that it does not call
   // database.AddPlayer(player);
   // It instead calls:
   // database.UpdatePlayer(player);
   await GetPlayerInfo(player.Name, player.ID, player.Type, true);
   }
  }

To visually update this, I clear all the rows on the DataGridView, lastly GetAllPlayers() and add all those players in a for loop by duplicating row[0] and editing it for each player.

The issue with this function is that it already took 7 seconds for only 21 Players. I think this time will increase drastically as more players are added in. So if I had ~120 Players, (which is my current goal), I'd be waiting for each update for 42 seconds. The program won't respond for those seconds, giving me doubts about it crashing etc. as well. What I await is generally a loading time from X seconds for Y Players. Realistic sounds 1 second for 20 Players, but my current result is yet far away from this.

0 Answers
Related