iphone - ASIHTTPRequest 下载缓存问题 - 无法在缓存中保存/加载日期

标签 iphone asihttprequest

我正在尝试使用 ASIHTTP ( http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_a_download_cache ) 将数据保存到缓存中。我不确定我做错了什么,但它似乎没有将数据存储到缓存中。以下代码的输出是这样的:

2010-08-12 15:44:13.801 TabBar[51728:207] Success is YES
2010-08-12 15:44:13.802 TabBar[51728:207] Success is NO
2010-08-12 15:44:13.804 TabBar[51728:207] Success is YES
2010-08-12 15:44:13.805 TabBar[51728:207] S-----------
2010-08-12 15:44:13.874 TabBar[51728:207] Success is YES
2010-08-12 15:44:13.874 TabBar[51728:207] Success is NO
2010-08-12 15:44:13.876 TabBar[51728:207] Success is YES

这是代码:

[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:
                              ASICacheForSessionDurationCacheStoragePolicy];
[[ASIDownloadCache sharedCache] 
                    setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL 
                                URLWithString:@"http://www.nytimes.com/"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request startSynchronous];

request = [ASIHTTPRequest requestWithURL:[NSURL 
                                URLWithString:@"http://www.nytimes.com/"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request startSynchronous];
BOOL success = ([request responseStatusCode] == 200);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = [request didUseCachedResponse];
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = ([[request responseData] length]);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

NSLog(@"S-----------");
request = [ASIHTTPRequest requestWithURL:[NSURL 
                    URLWithString:@"http://www.nytimes.com/"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request startSynchronous];
success = ([request responseStatusCode] == 200);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = [request didUseCachedResponse];
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

success = ([[request responseData] length]);
NSLog(@"Success is %@\n", (success ? @"YES" : @"NO"));

有人可以给我一些关于如何完成此任务的代码片段吗?

谢谢!

------------------------------------------------------------ ------------------------

新代码修订:

NSURL *url = [NSURL URLWithString:imageURL];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days

[request startSynchronous];//startAsynchronous];

BOOL success = [request didUseCachedResponse];
NSLog(@"------------>>>>>>> Success is %@\n", (success ? @"YES" : @"NO"));

NSData *responseData = [request responseData];
UIImage *image = [[UIImage alloc] initWithData:responseData];

这只适用于[request startSynchronous];如果我尝试异步方法,它不会调用此函数:

- (void)requestFinished:(ASIHTTPRequest *)request

最佳答案

你错过了这样的东西,

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days
[request setDelegate:self]; // A delegate must be specified
[request setDidFinishSelector:@selector(requestFinished:)]; // And an appropriate selector specified

你还应该有

- (void)requestFinished:(ASIHTTPRequest *)request {
  BOOL success = [request didUseCachedResponse];
  NSLog(@"------------>>>>>>> Success is %@\n", (success ? @"YES" : @"NO"));

  NSData *responseData = [request responseData];
  UIImage *image = [[UIImage alloc] initWithData:responseData];

  ...
}

这应该可以解决问题。

关于iphone - ASIHTTPRequest 下载缓存问题 - 无法在缓存中保存/加载日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3471529/

相关文章:

iphone使用以下功能

iphone - asihttp请求超时

iphone - 异步添加图像 - ASIHTTPRequest

ios4 - 在iPhone中使用ASIHTTPRequest将图像上传到服务器

ios - 检查 URL 中特定页面的可达性

iphone - 您能否仅使用 Core Animation 重新创建 iBooks 应用程序中看到的 3D 翻页效果,还是需要使用 OpenGL ES?

iphone - 标签栏的隐藏显示空白(即使框架是全屏的)

iphone - iphone有没有轻量级的http请求库?

ios - 如何在 Xcode 9 中在 iOS 设备上运行我的应用程序?

iphone - 避免 Facebook 单点登录