开发者

NSTimeInterval returning random values

开发者 https://www.devze.com 2023-02-05 06:03 出处:网络
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 (

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号