Skip to content

isMenuItemsStale shortcut issue #14

@bkai

Description

@bkai

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;
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions