Going Offline Errata

In Chapter 17: Going Offline, page 324
The code the clears the cached data files has a minor issue.

The code in the book is

for(NSString *path in cachedItems) {
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

The path variable in this code is just the file name and not the full path and as such, doesn’t delete the cached data file. Thanks to Brian for pointing this out.

You should use the following code instead.

for(NSString *path in cachedItems) {
NSString *fullPath = [[self cacheDirectory] stringByAppendingPathComponent:path];
[[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil];
}


Mugunth

3 comments

  1. Also I think on method:
    + (void)cacheData:(NSData*) data toFile:(NSString*) fileName;

    The part:


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

    Should be:


    if ([recentlyAccessedKeys count] > kCacheMemoryLimit) {
    NSString *leastRecentlyUsedDataFilename = [recentlyAccessedKeys lastObject];
    NSData *leastRecentlyUsedCacheData = [memoryCache objectForKey:leastRecentlyUsedDataFilename];
    // Changes: path yo save in disk the last object in memory
    NSString *archivePath = [[AppCache cacheDirectory] stringByAppendingPathComponent:leastRecentlyUsedDataFilename];
    // End changes.
    [leastRecentlyUsedCacheData writeToFile:archivePath atomically:YES];

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

    Thanks!
    Jose.

  2. Hi!
    In method isMenuItemsStale, the stalenessLevel is always returned negative.
    This is because the file modification date is always earlier than current date.
    I modified it to:
    NSDate *now = [NSDate date];
    NSDate *modificationDate = [[[NSFileManager defaultManager] attributesOfItemAtPath:archivePath error:nil] fileModificationDate];
    NSTimeInterval stalenessLevel = [now timeIntervalSinceDate:modificationDate];

    Does it make sense?

  3. amilnyleve

    Please revise chap 14 iHotel’s code – not working as in github

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>