|
Hi, I have a list that displays it's data from SwiftData. One of the items displayed is the number of days until a date stored in the model. It all seems to work fine except if you open the app the next day the number of days shown is incorrect as the data hasn't changed the today's date has. Currently I have to force quit the app and open it again. I can only think of a hacky way of solving this using a timer and updating a number in a ZStack than you cannot see but this obviously isn't the correct thing to do. Can anyone give me some guidance on the correct way of tackling this please I don't really want to have dirty hacks in the code. Thanks in advance, Gareth |
|
Am disappointed you didn't include code. How can we help you if we don't see what you did? Your SwiftData model doesn't have to only indicate what elements to store. It's a Swift class. I think you should consider adding business logic to your class. Here's a sample I wrote to illustrate the concept.
Keep Coding!Next time, help us to help you! Please include code snips along with inline comments explaining what you're trying to accomplish. PS: It's means "it is" or "it has" |
|
|
|
hi Gareth,
i'll try something different, since i think you're asking for help wih a slightly different question than @Obelix addresses. if your app goes into the background (e.g., you move to another), whatever screen you looked at previously in your app will be brought forward when you switch back to the app, but it will not update automatically. in your case, your sense of "what is today" when counting days until completion will be different if you come back to your app on another day. you can track when your app moves from active to inactive at the (SwiftUI) i have an example of exactly this in my shopping list app on GitHub ... it should not be hard to import. you'll find most of what you need in the just be sure that views always reference this "Today" object when counting rather than using Date.now(), which i am guessing is what you did. hope that helps, DMG |
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.