Using Parse, I'm trying to reduce the amount of network calls to parse in an effort to limit my API requests of nonessential data. To do this I thought it would be best to call a function only once a week on the same day and upload any differences from the previous week to the installation class (for syncing push notification channels in my circumstance)
So how do you call a function once a week every week on a particular day? And what do you if the day is already passed? So say you want something to happen every Thursday, but a user doesn't open the app until Sunday after that Thursday you were supposed to sync data?
In practice, I find that a fixed interval makes more sense in more cases than a calendar milestone. With that, and very little NSDate logic, I can have my model guarantee that its data is at most N seconds out of date.
To do this, I have the model singleton keep track only of the date of the last update:
The interface also provides an asynch update method, like:
I override the synthesized getter/setter of
lastUpdate
to wrap persistence:Finally, the asynch update opaquely decides whether the current data is good enough, or whether we should invoke the parse.com api...
The method
updatePeriod
answers whateverNSTimeInterval
the app thinks is an acceptable age of the data. Usually, I get this from parse.config at some reasonably high frequency (like daily). This way, I can tune the frequency of model updates as I see fit for clients in the wild.So with very little
NSDate
logic, I use this to keep the clients "up-to-date-enough", where even the "enough" part can be dynamically decided.EDIT - we can still remain concise and set our model expiration to be a calendar day. I'd do that as follows:
Now, instead of updating when the interval expires, update if the last update was before last Saturday (or whatever weekday, you want... adjust by adjusting setDay:-n)