-2

I have some code that needs to get called frequently, such as check what day it is, if it's the next day then move the day strings in the tableView.

Now I thought that the viewDidLoad would get called all the time and so it would be 'fine' to put it in there. However, I've left the simulator overnight, and I've pressed the home button and clicked again, changed VCs etc. and viewDidLoad hasn't been hit.

What are my options for doing sporadic checks such as, is it a new day? As x happened etc.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Jake Walker
  • 305
  • 2
  • 9
  • Call them when you need them... If you need periodic checking you can get it called when a controller appears or when application enters foreground or just have a timer there will call the check periodically in a given interval. – Sulthan Nov 03 '16 at 17:24
  • That's what I was trying to do, when a controller appears with the viewDidLoad, but obviously that hasn't worked – Jake Walker Nov 03 '16 at 17:47
  • There is a difference between "controller appears" (`viewWillAppear`) and controller is loaded (`viewDidLoad`). – Sulthan Nov 03 '16 at 18:17
  • viewWillAppear is exactly what I need, thanks a lot. I always forget about that, and just 'dump' everything in viewDidLoad – Jake Walker Nov 03 '16 at 18:31

3 Answers3

3

In this specific case, you can subscribe to NSCalendarDayChangedNotification to be notified when the date changes and respond accordingly in your view controller. In general, didBecomeActive or viewDidAppear would likely work.

Dare
  • 2,497
  • 1
  • 12
  • 20
1

What are my options for doing sporadic checks such as, is it a new day

It depends what the meaning of "is" is! In particular, "is" when? You say "sporadic", but that's just fluff. When do you need to know this? To what stimulus do you want to respond? When the user opens your app? Then put it in applicationDidBecomeActive. Every day at noon? Then run an NSTimer. Really, the problem here is that you don't seem to know, yourself, just when you need to perform these checks.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Basically, I want to do these checks when they click on the view controller, or reopen the app, so it is up to date. The NSTimer idea doesn't work because I believe you can't have a background timer if it's not an alarm application. – Jake Walker Nov 03 '16 at 17:45
  • Well, "reopen the app" is covered by what I said: `applicationDidBecomeActive`. I don't know what "click on the view controller" even means; a view controller is not something you can click on. But every time the view controller's _view_ reappears, you get (wait for it) `viewDidAppear`. So between those two we've just covered your whole spec, no? – matt Nov 03 '16 at 17:54
1

Whilst in your app, its quite easy to continually check for something. You simply create a background thread. However, what you describe is a thread that persists from outside the app's lifecycle.

Have a read on this documentation provided by Apple itself. You have to have good excuse to put a background thread. The scope of such thread is limited to only certain scenarios such as downloading background stuff, playing sounds etc.

For your scenario, I'd look at applicationDidBecomeActive(_:) found in your Application Delegate. There you can mimic such continual check. Beware however, don't put heavy word load on start up or your app might be killed automatically if it fails to become active in reasonable amount of time.

Evdzhan Mustafa
  • 3,645
  • 1
  • 24
  • 40
  • Thanks a lot, I think the applicationDidBecomeActive is where I should put it. I want it to be like when you click on the facebook or twitter app, the newsfeed automatically download new posts – Jake Walker Nov 03 '16 at 17:46