UWP - Compare data on JSON and database UWP - Compare data on JSON and database json json

UWP - Compare data on JSON and database


The problem is that you have two nested foreach loops. What the code does in simplified pseudocode:

For each item in JSON   Load all rows from DB   And for each loaded row      Check if the current JSON item matches the row from DB and if not, output

As you can see, if you have N items in the JSON and M rows in the database, this inevitably leads to N*M lines of output except for those rare ones where the JSON item matches a specific row in database.

If I understand it correctly, I assume that you instead want to check if there is a row that matches the JSON item and if not, output it. You could do this the following way:

List<String> title = sqlhelp.GetKomikData();HashSet<string> dbItems = new HashSet<string>();foreach (string juduldb in title){    judulbuku = juduldb.Substring(juduldb.IndexOf('.') + 1);    dbItems.Add( judulbuku );}...foreach ( JsonValue groupValue in jsonData1 ){    ...    //instead of the second foreach    if ( !dbItems.Contains( file1.Slug.Replace("-", "_") + ".pdf" ) )    {         //item is not in database    }    else    {         //item is in database    }}

Additional tips

  • Avoid calling GetKomikData inside the foreach. This method does not have any arguments and that means you are just accessing the database again and again without a reason, which takes time and slows down the execution significantly. Instead, call GetKomikData only once before the first foreach and then just use title variable.
  • Don't assign ItemsSource every time the collection changes. This will unnecessarily slow down the UI thread, as it will have to reload all the items with each loop. Instead, assign the property only once after the outer foreach
  • write your code in one language. When you start mixing variable names in English with Indonesian, the code becomes confusing and less readable and adds cognitive overhead.
  • avoid non-descriptive variable names like file1 or jsonObject1. The variable name should be clear and tell you what it contains. When there is a number at the end, it usually means it could be named more clearly.
  • use plurals for list variable names - instead of title use titles