简体   繁体   中英

NSTimeInterval returning random values

I'm writing a caching system for an app I'm working on. It stores a key (NSString) and a last requested timestamp (NSDate) in Core Data, and I'm having trouble seeing whether it's expired or not (it's set to expire after 300 seconds).

NSTimeInterval interval = [lastRequested timeIntervalSinceNow];
NSLog(@"Interval: %d", interval);
// reload every 5 minutes
if (interval > 300) {
  NSLog(@"Last Requested: %d (older than 5 minutes)", interval);
  return YES;
} else {
  NSLog(@"Last Requested: %d (within 5 minutes)", interval);
  return NO;
}

I've checked that lastRequested doesn't change - that's fine. But the interval produces a seemingly random number:

2011-01-19 11:31:03.770 App[2998:207] Interval: -830996480
2011-01-19 11:31:03.770 App[2998:207] Last Requested: -830996480 (within 5 minutes)
2011-01-19 11:30:57.634 App[2994:207] Interval: 1494482944
2011-01-19 11:30:57.634 App[2994:207] Last Requested: 1494482944 (within 5 minutes)
2011-01-19 11:30:51.219 App[2991:207] Interval: -1951399936
2011-01-19 11:30:51.219 App[2991:207] Last Requested: -1951399936 (within 5 minutes)

And bizarrely, as you can see when checking whether the value is greater than 300, in the middle run of the app - it's still thinking it's less than 300 even though it's 1494482944.

Any ideas? Thanks.

When you log your interval, you log it as a integer (%d) but a time interval is a double (%f). Replace those instances in your NSLog(...)s and you should see the correct values.

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