Search code examples
iosobjective-cviewdidload

How can I avoid this "if" block in viewDidLoad to be executed?


First sorry for not being accurate in the title, but I will try to describe my issue as precise as possible.

I am developing an app to manage costs/expenses. In this case I have this block in the viewdidload method:

Piece of code about what I got so far:

-(void)viewDidLoad {
[super viewDidLoad];

 if([[self getfrequencyselected] isEqualToString:@"Daily"] && [self limitneedstoberaised]){
  NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];

        switch ([component weekday]) {
              case 1:[self raisedaylimit];
                    break;
              case 2: [self raisedaylimit];
                    break;
               --- etc,etc ---
             default:
                    break;
        }

  }
}

 -(BOOL)limitneedstoberaised{
  /*

   Something like this: 

   If today's date is the same as the date stored in the SQLite DB (LASTUPDATEDATE column), 
   then return FALSE, else return TRUE;
   [[NSCalendar currentCalendar] isDate:date1 inSameDayAsDate:date2];


  */

 }

- Basically if the user has previously selected to keep track of costs in a daily basis, enter in that switch case. - In the switch case 1, 2, etc represent the days of the week, so it should raise the specified cost limit every day that has passed.

My problem:

This block is in the viewdidload, so it will be executed every time the app is launched.

My problem is that [self raisedaylimit] will be, therefore, called every time the user launches the app, so I would like to avoid that, by just to be executed once per day, as the logic says.

Possible solution:

  • I have thought about creating a NSUserDefaults object to keep track when the raisedaylimit method is called, toggling between YES and NO, but that seems complicated.
  • I have thought that maybe this block will need to be outside of the viewdidload method so that the block is executed just when it's needed

Solution

  • Your first thought is correct, you need to store the day you last raised the limit outside of your app - and in user defaults is the obvious place.

    Your code will read the stored date, get the current date, and calculate the number of days between them – read the NSDate documentation, there are methods which will determine the difference between two dates to some granularity (hours, days, etc.), you do not need to reduce an NSDate value by stripping off the time component yourself before do the difference calculation.

    Call your code on app launch. If you need to allow for your app running past midnight and needing to raise the limit a second (or third etc.) time your code should calculate the time left till midnight and set an NSTimer to call itself again.

    If you get stuck designing your solution ask a new question with the code you have developed, what your problem is, etc. and someone will undoubtedly help you at that point.