NSData category for async loading data from url and calling block. Requested data is cached with NSCache and can be requested multiple times simultaneously.
Objective-C
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
SAMCache @ 2353729
.gitignore
.gitmodules
LICENSE
NSData+AsyncCacher.h
NSData+AsyncCacher.m
README.md

README.md

NSData-AsyncCacher

NSData category for async loading data from url and calling block. Requested data is cached with NSCache and can be requested multiple times simultaneously.

You need no more care about:

  • cache responses
  • several same requests simultaniuosly

This is great for downloading repeatable images like avatars!

Example:

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = // . . .
    
    [NSData getDataWithContentsOfURL:[NSURL URLWithString:urlString]
                             toBlock:^(NSData * data, BOOL * retry)
    {
        if (data == nil) {
           *retry = YES;
           return;
        }
        
        // cell may be nil if you scroll away
        UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
       	cell.imageView.image = [UIImage imageWithData:data];
    }];
    
    return cell;
}