Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a View has loaded since midnight

I have a ViewController that performs a random shuffle on an array and spits out text to a Label (in the viewDidLoad method). The problem is that whenever I navigate to the same ViewController it performs the shuffle again and I only need it to shuffle ONCE every day.

So I need to check if this ViewController has been loaded before on the SAME day (i.e. since midnight) and I can then put the shuffle into an if statement. Could I schedule the shuffle at midnight regardless of the app being open or not?

I have looked into setting a boolean to NSUserDefaults: something like hasLoadedSinceMidnight but then can't work out how to RESET the boolean at midnight.

like image 942
RoshDamunki Avatar asked Mar 19 '13 18:03

RoshDamunki


1 Answers

You could implement the AppDelegate's significantTimeChange method:

-(void)applicationSignificantTimeChange:(UIApplication *)application {
    //tell your view to shuffle
}

This method is called every midnight and during significant time changes such as a time zone change. If your app is closed when the event is received, this method will be called when your app is next opened.

More information can be viewed here

An additional way to do the same thing inside of your ViewController instead of in the AppDelegate would be to add:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:UIApplicationSignificantTimeChangeNotification object:nil];

and then you can just perform your shuffle operation in the -(void)performAction; method of that ViewController.

like image 138
Jsdodgers Avatar answered Oct 21 '22 15:10

Jsdodgers