Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isMenuItemsStale shortcut issue #14

Open
bkai opened this issue May 14, 2013 · 0 comments
Open

isMenuItemsStale shortcut issue #14

bkai opened this issue May 14, 2013 · 0 comments
Assignees

Comments

@bkai
Copy link

bkai commented May 14, 2013

Talking about ch24/iHotelApp/iHotelApp/ directory.

What is the reason for declaring, that if data is in memory cache, it is not stale? As in AppCache.m:

+(BOOL) isMenuItemsStale
{
  // if it is in memory cache, it is not stale
  if([recentlyAccessedKeys containsObject:@"MenuItems.archive"])
    return NO;
...
}

Below I have extracted all code excerpts to visualize at least two unclear points with this declaration, each of which, when confirmed, would invalidate it.

Point 1:
Data can be in the memory long enough (i.e. longer than kMenuStaleSeconds) to get stale.

Point 2:
Prior to call [AppCache isMenuItemsStale] in the view controller viewWillApear method there is a call to [AppCache getCachedMenuItems] which inserts the data into memory cache and also into recentlyAccessedKeys in case it was not there. Call to [AppCache isMenuItemsStale] thus returns NO in all cases.

I think the whole if statement should be removed.

Thanks!

iHotellAppMenuViewController.m:

-(void) viewWillAppear:(BOOL)animated {

  self.menuItems = [AppCache getCachedMenuItems];  
  [self.tableView reloadData];

  if([AppCache isMenuItemsStale] || !self.menuItems) {

    [AppDelegate.engine fetchMenuItemsOnSucceeded:^(NSMutableArray *listOfModelBaseObjects) {

      self.menuItems = listOfModelBaseObjects;
      [self.tableView reloadData];
    } onError:^(NSError *engineError) {
      [UIAlertView showWithError:engineError];
    }];
  }

  [super viewWillAppear:animated];
}

AppCache.m:

+(NSMutableArray*) getCachedMenuItems
{
  return [NSKeyedUnarchiver unarchiveObjectWithData:[self dataForFile:@"MenuItems.archive"]];
}

+(NSData*) dataForFile:(NSString*) fileName
{
  NSData *data = [memoryCache objectForKey:fileName];  
  if(data) return data; // data is present in memory cache

    NSString *archivePath = [[AppCache cacheDirectory] stringByAppendingPathComponent:fileName];
  data = [NSData dataWithContentsOfFile:archivePath];

  if(data)
    [self cacheData:data toFile:fileName]; // put the recently accessed data to memory cache

  return data;
}

+(void) cacheData:(NSData*) data toFile:(NSString*) fileName
{
  [memoryCache setObject:data forKey:fileName];
  if([recentlyAccessedKeys containsObject:fileName])
  {
    [recentlyAccessedKeys removeObject:fileName];
  }

  [recentlyAccessedKeys insertObject:fileName atIndex:0];

  if([recentlyAccessedKeys count] > kCacheMemoryLimit)
  {
    NSString *leastRecentlyUsedDataFilename = [recentlyAccessedKeys lastObject];
    NSData *leastRecentlyUsedCacheData = [memoryCache objectForKey:leastRecentlyUsedDataFilename];
    NSString *archivePath = [[AppCache cacheDirectory] stringByAppendingPathComponent:fileName];  
    [leastRecentlyUsedCacheData writeToFile:archivePath atomically:YES];

    [recentlyAccessedKeys removeLastObject];
    [memoryCache removeObjectForKey:leastRecentlyUsedDataFilename];
  }
}

+(BOOL) isMenuItemsStale
{
  // if it is in memory cache, it is not stale
  if([recentlyAccessedKeys containsObject:@"MenuItems.archive"])
    return NO;

    NSString *archivePath = [[AppCache cacheDirectory] stringByAppendingPathComponent:@"MenuItems.archive"];  

  NSTimeInterval stalenessLevel = [[[[NSFileManager defaultManager] attributesOfItemAtPath:archivePath error:nil] fileModificationDate] timeIntervalSinceNow];

  return stalenessLevel > kMenuStaleSeconds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants