How to call a function only one time per week or month?
I am asking myself how to call a function only one time per week or month? I thought about NSTimer, but that only works during the app is active, doesn't it?
So maybe I have to save t开发者_Python百科he current time and determine the past days every time I start the app. Or is there a simpler method, like a simple function call? ;-)Help appreciated
Yes, you need to store your last synced date in database/user defaults, and then need to fetch from database/user defaults
and need to pass both date, synced date and current date to following function
-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today {
NSDate *startDate = lastDate;
NSDate *endDate = today;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
int days = [components day];
return days;
}
and then you need to check how many days have passed
if([self howManyDaysHavePast:lastSyncedDate today:[NSDate date]]>=7)
{
[self callFunction];
}
精彩评论