TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Automatic data download and view refresh using timer?

Forums > SwiftUI

First post here so apologies in advance for any guidelines I might break inadvertently.

I have a simple monitor app which downloads a json dataset to populate a table.

By pulling down, I can manually trigger a refresh which calls the download function.

What I am trying to achieve is automatic download every 10 minutes but failing miserably!

I have tried two methods:

1) using a timer with .onreceive on the view to call my download function 2) using a timer to directly call the download function

The error I hit is that the onreceive and the timer are expecting synchronous functions but my download function is async.

I would be really grateful if anyone could give me some pointers regarding wrapping my async download function so it can be called from the .onreceive of the view.

Alternatively a pointer towards a pattern for implementing automatic period downloads.

Many thanks!

2      

Does

Task {
  // your function in here
}

not work?

But keep in mind that as soon as your app goes into background (on iOS) your timer shouldn't work anymore because the app is suspended in background. You need to configure background modes for such behaviour.

https://developer.apple.com/documentation/xcode/configuring-background-execution-modes

2      

Thank you so much! I've spent hours trying to get this working.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.