Darren Jones Profile picture
iOS/Mac developer. Creator of Snaphack, Iconical, DD-WRT, VAT Making Tax Digital and now Pass On apps among many others.

Sep 27, 2020, 11 tweets

A few things I’ve learnt about widgets whilst trying to add live bus times (a thread):
How often can a widget make a URLRequest to refresh its data? #iosdev 1/11

Fetching data then setting 1 solo view in the timeline and asking for a refresh in 2 minutes, will not fire for approx 5 minutes.
So 5 minutes then?
Well no. 2/11

If you add say 18 views to the timeline 10 seconds apart (3 mins) and request an update after 2 minutes, you will likely get a hit every 2 minutes and be able to call your URLRequest. 3/11

2 minutes is an ok refresh time for me displaying when the next bus is due. 5 minutes wouldn’t be as a lot can change in 5 minutes.
How do you make a widget look like it’s updated even though it hasn’t? 4/11

Well if I know the times the next few buses are due, I can use the 10 second timeline entries to show an updated ‘Due in xx’ time. A problem I faced is that all 18 timeline views are generated at the same time! 5/11

So if you use Date() in your calculations your views will all have the same data. So you have to your use date variable from your timeline entry when calculating times. With this my widget looks like it’s showing live data and the due times update in real time. 6/11

Can’t you just use the new Date() formatters for widgets?
Yes and these work great. Add a date to a widget and the clock can run up or down in real time. I use this for my ‘Last updated:’ label. The problem I had with this is you cannot use a custom format on the display. 7/11

It’s all (HH:mm:ss) or nothing.I don’t want to display seconds to the user as the live bus data is all set to 00 seconds so only accurate to the minute. Showing a user their bus is 15 seconds away will likely make them take it as gospel and think it’s accurate to the second. 8/11

So I had to use basic labels and update every 10 seconds.
One final thing I haven’t implemented yet, the refresh times may not happen when you expect. You can mostly get 2 minute updates but I’ve seen times of 15 minutes with no update with no known reason why. 9/11

I plan to add a view after 3 minutes, once I have no timeline entries left, telling the user to tap to open the app. You can force refresh your widget from within the app as often as you like, so I fire a refresh when the main app goes into the background. 10/11

I’m very happy with the result and once a bus’s due time was over a minute ago it’s removed from the stack and the next one shown, even if I haven’t been able to get a URLRequest with new data. 11/11

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling