Showing posts with label NSCache example. Show all posts
Showing posts with label NSCache example. Show all posts

Tuesday, May 7, 2013

NSCache setTotalCostLimit example ios


setTotalCostLimit:

Sets the maximum total cost that the cache can have before it starts evicting objects.
- (void)setTotalCostLimit:(NSUInteger)lim
Parameters of [NSCache setTotalCostLimit]
lim
The maximum total cost that the cache can have before it starts evicting objects.
Example of [NSCache setTotalCostLimit]
cache = [[NSCache alloc] init];
[cache setCountLimit:100];
[cache setTotalCostLimit:1500000];
[cache setEvictsObjectsWithDiscardedContent:YES];

NSCache totalCostLimit example ios


totalCostLimit

Returns the maximum total cost that the cache can have before it starts evicting objects.
- (NSUInteger)totalCostLimit
Return Value
The current maximum cost that the cache can have before it starts evicting objects.
Discussion of [NSCache totalCostLimit]
The default value is 0, which means there is no limit on the size of the cache. If you add an object to the cache, you may pass in a specified cost for the object, such as the size in bytes of the object. If adding this object to the cache causes the cache’s total cost to rise above totalCostLimit, the cache could automatically evict some of its objects until its total cost falls below totalCostLimit. The order in which the cache evicts objects is not guaranteed. This limit is not a strict limit, and if the cache goes over the limit, an object in the cache could be evicted instantly, at a later point in time, or possibly never, all depending on the implementation details of the cache.
Example of [NSCache totalCostLimit]
cache = [[NSCache alloc] init];
[cache setCountLimit:100];
[cache setTotalCostLimit:1500000];
[cache setEvictsObjectsWithDiscardedContent:YES];

NSCache setObject example ios


setObject :forKey:

Sets the value of the specified key in the cache.
- (void)setObject:(id)obj forKey:(id)key
Parameters
obj
The object to be stored in the cache.
key
The key with which to associate the value.
Discussion of [NSCache setObject]
Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.
Example of [NSCache setObject]

id object = [NSString stringWithFormat:@"%lu", theIndex % 10];
id key = [NSString stringWithFormat:@"sdffffffff%lu", theIndex];
[self.cache setObject :object forKey:key];
Example of [NSCache setObject]
// Your cache should have a lifetime beyond the method or handful of methods
// that use it. For example, you could make it a field of your application
// delegate, or of your view controller, or something like that. Up to you.
NSCache *myCache = ...;
NSAssert(myCache != nil, @"cache object is missing");

// Try to get the existing object out of the cache, if it's there.
Widget *myWidget = [myCache objectForKey: @"Important Widget"];
if (!myWidget) {
    // It's not in the cache yet, or has been removed. We have to
    // create it. Presumably, creation is an expensive operation,
    // which is why we cache the results. If creation is cheap, we
    // probably don't need to bother caching it. That's a design
    // decision you'll have to make yourself.
    myWidget = [[[Widget alloc] initExpensively] autorelease];

    // Put it in the cache. It will stay there as long as the OS
    // has room for it. It may be removed at any time, however,
    // at which point we'll have to create it again on next use.
    [myCache setObject: myWidget forKey: @"Important Widget"];
}

// myWidget should exist now either way. Use it here.
if (myWidget) {
    [myWidget runOrWhatever];
}

NSCache setEvictsObjectsWithDiscardedContent example ios


setEvictsObjectsWithDiscardedContent:

Sets whether the cache will automatically evict NSDiscardableContent objects after the object’s content has been discarded.
- (void)setEvictsObjectsWithDiscardedContent:(BOOL)b
Parameters
b
If YES, the cache evicts NSDiscardableContent objects after the object’s contents has been discarded; if NO the cache does not evict these objects.

Example of [NSCache setEvictsObjectsWithDiscardedContent]
   _cellCache = [[NSCache alloc] init];
   [_cellCache setDelegate:self];
   [_cellCache setEvictsObjectsWithDiscardedContent :NO];

NSCache setCountLimit example ios


setCountLimit:

Sets the maximum number of objects that the cache can hold.
- (void)setCountLimit:(NSUInteger)lim
Parameters
lim
The maximum number of objects that the cache will be allowed to hold.
Discussion of [NSCache setCountLimit]
Setting the count limit to a number less than or equal to 0 will have no effect on the maximum size of the cache.

Example of [NSCache setCountLimit]
cache = [[NSCache alloc] init];
[cache setCountLimit:100];
[cache setTotalCostLimit:1500000];
[cache setEvictsObjectsWithDiscardedContent:YES];

Example of [NSCache setCountLimit]
+(NSCache*)sharedCache;
{
    static NSCache* sharedCache = nil;
    if (sharedCache == nil) {
        sharedCache = [[[self alloc] init] retain];
        [sharedCache setCountLimit:100];
    }
    return sharedCache;
}

NSCache objectForKey example ios


objectForKey:

Returns the value associated with a given key.
- (id)objectForKey:(id)key
Parameters
key
An object identifying the value.
Return Value of [NSCache objectForKey]
The value associated with key, or NULL if no value is associated with key. The caller does not have to release the value returned to it.
Example of [NSCache objectForKey]
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
 NSString *res = [cache objectForKey :indexPath];
 if (!res) {                      
     res=@"1";
     [cache setObject:res forKey:indexPath];
 }
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
NSLog(@"%@",[cache objectForKey:indexPath]);
Example of [NSCache objectForKey]
// Your cache should have a lifetime beyond the method or handful of methods
// that use it. For example, you could make it a field of your application
// delegate, or of your view controller, or something like that. Up to you.
NSCache *myCache = ...;
NSAssert(myCache != nil, @"cache object is missing");

// Try to get the existing object out of the cache, if it's there.
Widget *myWidget = [myCache objectForKey: @"Important Widget"];
if (!myWidget) {
    // It's not in the cache yet, or has been removed. We have to
    // create it. Presumably, creation is an expensive operation,
    // which is why we cache the results. If creation is cheap, we
    // probably don't need to bother caching it. That's a design
    // decision you'll have to make yourself.
    myWidget = [[[Widget alloc] initExpensively] autorelease];

    // Put it in the cache. It will stay there as long as the OS
    // has room for it. It may be removed at any time, however,
    // at which point we'll have to create it again on next use.
    [myCache setObject: myWidget forKey: @"Important Widget"];
}

// myWidget should exist now either way. Use it here.
if (myWidget) {
    [myWidget runOrWhatever];
}

NSCache evictsObjectsWithDiscardedContent example ios


evictsObjectsWithDiscardedContent

Returns whether or not the cache will automatically evict discardable-content objects whose content has been discarded.
- (BOOL)evictsObjectsWithDiscardedContent
Return Value of [NSCache evictsObjectsWithDiscardedContent]
YES if the cache will evict the object after it is discarded; otherwise, NO.
Discussion
By default, evictsObjectsWithDiscardedContent is set to YES.
Example of [NSCache evictsObjectsWithDiscardedContent]
   _cellCache = [[NSCache alloc] init];
   [_cellCache setDelegate:self];
   [_cellCache setEvictsObjectsWithDiscardedContent:NO];

NSCache countLimit example ios


countLimit

Returns the maximum number of objects that the cache can currently hold.
- (NSUInteger)countLimit
Return Value
The maximum number of objects that the cache can currently hold.
Discussion of [NSCache countLimit]
By default, countLimit will be set to 0. Any countLimit less than or equal to 0 has no effect on the number of allowed entries in the cache. This limit is not a strict limit, and if the cache goes over the limit, an object in the cache could be evicted instantly, later, or possibly never, all depending on the implementation details of the cache.
Example of [NSCache countLimit]


- (NSCache *)imageCache {

    if (!_imageCache) {
        _imageCache = [[NSCache alloc] init];
        _imageCache.countLimit = kIMAGE_REQUEST_CACHE_LIMIT;
    }
    return _imageCache;
}
Example of [NSCache countLimit]
-(UIImage *)buscarEnCache:(UsersController *)auxiliarStruct{
    UIImage *image;

    NSLog(@"cache: %i", [cache countLimit]);
    if ([cache countLimit] > 0) {
        if ([cache objectForKey:auxiliarStruct.thumb]){    
            image = [cache objectForKey:auxiliarStruct.thumb];
        }else{ //IF isnt't cached, is saved
            NSString *imageURLString = [NSString stringWithFormat:@"http://mydomain.com/%@",auxiliarStruct.thumb];
            NSURL *imageURL = [NSURL URLWithString:imageURLString];
            NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
            image = [UIImage imageWithData:imageData];
            [cache setObject:image forKey:auxiliarStruct.thumb];
        }        
    }else{ 
        NSString *imageURLString = [NSString stringWithFormat:@"http://mydomain.com/%@",auxiliarStruct.thumb];
        NSURL *imageURL = [NSURL URLWithString:imageURLString];
        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        image = [UIImage imageWithData:imageData];
        [cache setObject:image forKey:auxiliarStruct.thumb];
    }
    return image;
}