Widgets calling WeatherService.shared.weather multiple times at the same time

Hello. I noticed pretty early on that WeatherService.shared.weather seems to cache results. I realized this when I called it, killed network connectivity to my device, and then called it again I still got results back.

My concern is about my widgets, which are also able to call WeatherService.shared.weather. I put my call inside of getTimeline(). After adding logging I realized that getTimeline() is called multiple times at the same time. For example, if I have two different widgets each with three different sizes (small, large, rectangular, etc) then six calls to getTimeline() are made for an update. I found a blog by Shopify iOS developers that describes how they found the same thing, but the forums aren't letting me link to their website's blog.

My questions is which of these scenarios is happening:

  1. Even though WeatherService.shared.weather is called six times in the same second it's actually only the first call that goes out via the network and the remaining five calls just returned cached results.

  2. More than one of the six calls in the same second could be making a network call. If that's the case I'm going to need to add some form of throttling/caching because the number of free API calls per month is limited.

Thank you!

Replies

Scenario 1 is happening. WeatherService will do the caching for you.