Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nmills3 opened this issue Sep 8, 2023 · 4 comments
Open

Comments

@nmills3
Copy link

nmills3 commented Sep 8, 2023

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

@jtwotimes
Copy link
Owner

Can you share the code you have?

@nmills3
Copy link
Author

nmills3 commented Sep 12, 2023

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
Copy link
Author

nmills3 commented Oct 17, 2023

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
Copy link
Author

nmills3 commented Oct 17, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants