PokeApiNet
PokeApiNet copied to clipboard
Removing an item from a page resource also removes it from the cache
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
Can you share the code you have?
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
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
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