简体   繁体   中英

NSDate date not returning current date and time after waking

I've noticed that [NSDate date] returns old date and times soon after waking from sleep. I can't find any information around the specification of this, or how to refresh the current date and time after waking.

Does anyone have any information on the behavior?

Are you saving NSDate in an ivar and checking it after the app is relaunched? It'll be the old date if this is the case.

You could re-initialize the ivar on viewWillAppear , or do it in your AppDelegate . Alternatively you can also set it to nil on exit.

Perhaps try handling the applicationSignificantTimeChange: app delegate method or the UIApplicationSignificantTimeChangeNotification notification.

From the applicationSignificantTimeChange: documentation:

If your application is currently suspended, this message is queued until your application returns to the foreground, at which point it is delivered. If multiple time changes occur, only the most recent one is delivered.

It may be that you have to wait until this point before you can get accurate data from the NSDate class. Your views are probably being loaded too early.

It's probably better to do this kind of thing in viewWillAppear: however, and I have a feeling if you do this you can avoid dealing with the significant time change events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM