Domanda

Its my first time using NSCache and I know that it doesn't persist on app lunch.

But does it persist across ViewControllers?... Meaning... If I set the cache object on ViewController A and then I move to ViewController B can I still access it?

My question is related to an issue that I am having in my code. I am on ViewController B and I set the cache object. Then move to ViewController B and try to retrieve that object but is never found.

Is that normal or there is a problem in my code??? My Views are quite inexpensive so I see no reason why it would be dropping the cache object

ViewController A (Using Cache)

- (void) searchDone:(NSDictionary*)response {

    NSString * str = input.text;
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString* cachedKey = [str stringByReplacingOccurrencesOfString:@"#" withString:@""];
    cachedKey = [cachedKey lowercaseString];

    //
    // Cache
    //
    NSCache * cache = [[NSCache alloc]init];
    NSDictionary* chachedData = [cache objectForKey:cachedKey];

    // Check for a cached version of this
    if ( chachedData ) {

        NSLog(@"There is a cache");

        NSTimeInterval timeDifferenceBetweenDates = [chachedData[@"time"] timeIntervalSinceNow];
        CGFloat time = fabsf(timeDifferenceBetweenDates);

        if ( time < sysTraditionalSearchCacheTime ) {
            NSLog(@"using cache");
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:input.text,@"input",chachedData,@"response",nil];

            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"closeSearch"
             object:nil
             userInfo:dictionary];
            return;
        }
        [cache removeObjectForKey:cachedKey];
    }

ViewController B (Cache Setter)

- (void) notificationCloseSearch:(NSNotification*) notification {

    input.text = [[notification userInfo] valueForKey:@"input"];

    NSDictionary* response = [[notification userInfo] valueForKey:@"response"];
    NSString * str = input.text;
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString* cachedKey = [str stringByReplacingOccurrencesOfString:@"#" withString:@""];
    cachedKey = [cachedKey lowercaseString];
    //
    // Cache
    //
    NSCache * cache = [[NSCache alloc]init];
    NSDictionary* chachedData = [cache objectForKey:cachedKey];

    // Check for a cached version of this
    if ( chachedData ) {
        NSTimeInterval timeDifferenceBetweenDates = [chachedData[@"time"] timeIntervalSinceNow];
        CGFloat time = fabsf(timeDifferenceBetweenDates);
        if ( time >= sysTraditionalSearchCacheTime ) {
            [cache removeObjectForKey:cachedKey];
        }
    } else { // if there is no cache then set one
        NSLog(@"setting cache key %@",cachedKey);
        NSDate* now = [NSDate date];
        NSMutableDictionary* newResopnse = [[NSMutableDictionary alloc]initWithDictionary:response];
        [newResopnse setObject:now forKey:@"time"];
        response = [[NSDictionary alloc] initWithDictionary:newResopnse];
        [cache setObject:response forKey:cachedKey];
        NSLog(@"cached %@",[cache objectForKey:cachedKey]);
    }
}
È stato utile?

Soluzione

NSCache is a some kind of NSMutableDictionary. The difference is that when NSCache detects excessive memory pressure it will release some of it key-value pairs. In ViewControllerA you create NSCache object

NSCache * cache = [[NSCache alloc]init];

And in ViewControllerB you create one more NSCache object again

NSCache * cache = [[NSCache alloc]init];

So it will be two different objects with two different set of key-value pairs. If you need some kind of storage you can write singleton class which will contain one NSCache object.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top