Skip to content

Instantly share code, notes, and snippets.

@gorenje
Created March 11, 2011 15:14
Show Gist options
  • Save gorenje/865994 to your computer and use it in GitHub Desktop.
Save gorenje/865994 to your computer and use it in GitHub Desktop.
cib data caching based on urls. This is simplest case, no loadDelegate is supported.
/*
* Store all CIB data on a url-data basis in this dictionary. To prevent cibs from
* being continously downloaded by the CPWindowController, or rather the CPCib, we
* override the responsible init method and cache the data.
*/
CibDataCacheDictionary = [CPDictionary dictionary];
@implementation CPCib (CacheDataResponse)
/*
* The argument, at least in the current application, always seems to be a CPString.
* So we can assume that, and append a timestamp to avoid caching problems.
*/
- (id)initWithContentsOfURL:(CPURL)aURL
{
self = [super init];
if (self)
{
if ( [CibDataCacheDictionary objectForKey:aURL] ) {
_data = [CPData dataWithRawString:[[CibDataCacheDictionary
objectForKey:aURL] rawString]];
} else {
var request = [CPURLRequest
requestWithURL:[aURL stringByAppendingFormat:"?%s",
[CPString timestamp]]];
_data = [CPURLConnection sendSynchronousRequest:request returningResponse:nil];
[CibDataCacheDictionary setObject:_data forKey:aURL];
}
_awakenCustomResources = YES;
}
return self;
}
@end
@implementation CPString (IsBlank)
+ (CPString)timestamp
{
return [CPString stringWithFormat:"%d", new Date().getTime()];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment