kampbell411 February 2016

Refreshing an App's JSON content once every new day in Swift

I have an app that parses a couple random strings using JSON and I would like the app to reload and show new content everyday instead of someone looking at old content from the day before. Just refreshing once to show the new days content. Kinda of like the "quote of the a day" apps out there.

I have looked into Background Fetch and was wondering if I am overcomplicated it and if its possible to just compare a date somehow and reload the JSON session. Any direction is greatly appreciated.

Answers


Antoine Garcia February 2016

You can use a static variable or NSUserDefault if you want it to be persistent to store the date and compare with the current date :

let refreshTime = 24.0 * 60.0 * 60.0 //for a refresh every 24 hours
static var lastFetchedDate : NSDate? = nil

if lastFetchedDate == nil || lastFetchedDate.timeIntervalSinceNow * -1.0 > refreshTime{
   //RELOAD JSON
  lastFetchedDate = NSDate()
}

Post Status

Asked in February 2016
Viewed 3,552 times
Voted 7
Answered 1 times

Search




Leave an answer