0

I have a List view that the user can add items.The basic idea I wanted to achieve is; "After 24 hours, reset the list to an empty state./ Delete all the items in the list after 24 hours."

I want this list to be empty after 24 hours. Perhaps the app could check the time the user first accessed the app and see if 24 hours have passed since then. Alternatively, the app may check the current date and see if it has changed.

I usually use the code below to check if the list is empty or not:

if items.count == 0

This is how I show the items in the List view:

List { ListRowItemView(item: item) }

I thought about adding an onAppear action to the List view since I think that it is not possible to refresh the app in the background if the app is not opened or etc. But I don't know how to create a 24 hours countdown that will set the items.count to 0

I would appreciate the help!

mamamumumo
  • 21
  • 3
  • Save the time the list was last accessed in UserDefaults and check that time in onAppear as you mentioned – Joakim Danielson Sep 17 '22 at 20:30
  • @JoakimDanielson Hey, thanks for the answer. However I’m a beginner and I’m not sure about how to use UserDefaults in that way, would you be able to explain it like as if I’m five years old hahah – mamamumumo Sep 17 '22 at 22:46
  • if you have the intellect of a 5 years old, then have a look at these links, for how to use `UserDefaults`, https://developer.apple.com/documentation/foundation/userdefaults and https://stackoverflow.com/questions/31203241/how-can-i-use-userdefaults-in-swift and https://www.simpleswiftguide.com/how-to-use-userdefaults-in-swift/ – workingdog support Ukraine Sep 17 '22 at 22:56

0 Answers0