PokeApiNet icon indicating copy to clipboard operation
PokeApiNet copied to clipboard

Removing an item from a page resource also removes it from the cache

Open nmills3 opened this issue 1 year ago • 4 comments

So i have a method that gets all pokemon from pokeapi using a call to GetNamedResourcePageAsync and then loops over the results and removes any results that do not match a given filter. When I call GetNamedResourcePageAsync again, instead of all pokemon being returned, only the pokemon that were left after the previous filter are left. It seems like removing items from the results list is also removing them from the cache

nmills3 avatar Sep 08 '23 15:09 nmills3

Can you share the code you have?

jtwotimes avatar Sep 12 '23 18:09 jtwotimes

something simple like this does it

using PokeApiNet;

namespace PokeApiBugExample
{
	internal class Program
	{
		static async Task Main(string[] args)
		{
			PokeApiClient client = new PokeApiClient();
			var pokemon = await client.GetNamedResourcePageAsync<Pokemon>(int.MaxValue, 0);
			Console.WriteLine(pokemon.Results.Count); //1281

			for (int i = 0; i < 100; i++)
			{
				pokemon.Results.Remove(pokemon.Results[0]);
			}

			var pokemon2 = await client.GetNamedResourcePageAsync<Pokemon>(int.MaxValue, 0);
			Console.WriteLine(pokemon2.Results.Count); //1181

			Console.ReadLine();
		}
	}
}

as you see, the second call only returns 1181 items instead of the expected 1281 as removing them from the "pokemon" list seems to also remove them from the cache

edit: i have no idea how to get this to format properly. Sorry edit 2: fixed it

nmills3 avatar Sep 12 '23 20:09 nmills3

you put around the code block instead of and then it formats properly. edit: put 3 of the tilde things on either side of the code block instead of just one on either side of the code block, then it formats properly

nmills3 avatar Oct 17 '23 06:10 nmills3

edit 2: fixed it

Glad you found the fix and shared it with us

you can also fix the cache issue by just cloning the lists before removing items from them. so then you're removing items from the clone list instead of the cached list. but that doesn't solve the underlying issue

nmills3 avatar Oct 17 '23 08:10 nmills3