How to Optimize my API calls?
Trying to get better at some C# with WPFUI, and started a project, is kinda of an imdb application. Showing TV shows, movies, info on both and actors etc.
Im using an API to get all the latest movies from one place, and get the ID's from that JSON list. Putting them into an List.
And then load all the movie info from another API(TMDB), using the IDs from the first.
Its working just fine, but when loading, it takes like 5-10sec before anything is being shown.
And when scrolling, its starting to load the new posters, so that takes another 3-5 sec before loading. If i then scroll back up, its loading the first rows again, taking another 3-5 sec.
It seems kinda weird and im not sure how to get the scrolling fixed, and how to optimize the API calls, so it does not take 5-10 sec for the info to load.
Can this be done in a better way then what i have done?
I have created a Gist how give a better view of the code:
https://gist.github.com/PatrickRorth/4b634a10eb4a47bd7621d159cfa73d0e
I have also added a small recording, showing how it reloads images again after scrolling.
Any help would be appriciated.
9 Replies
one thing that stands out to me is this section:
specifically the await inside a foreach loop
if we assume each call to GetMovieInfo takes one second, and you have ten movies to go through, you're locked-in to at least ten seconds of downtime
instead you should consider using
await Task.WhenAll(...)
to await all of that i/o at once
it would look something like this:
as for the scrolling issue, you probably just want to cache your results
rather than writing directly to your viemodel property (TMDBmovies)you may find this snippet from david fowler useful: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md
GitHub
AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master · davidfow...
This repository has examples of broken patterns in ASP.NET Core applications - davidfowl/AspNetCoreDiagnosticScenarios
oh nice!
the key parts being the AsyncLazy class he creates and uses
I load in around 50 movies from the VIDSRCAPI, is that to much?
i would like a full overview, and not just load 5 at at time
each API has their own rate limits you need to follow
but speaking informally, 50 requests is not a lot at all
so i think you would be fine to just load them all at once
nice niec!
Ill def. try that! Thank you so much!
no prob 🙂
have fun
thanks, i sure will! 😄