ios – ASIHTTPRequest – 缓存中的数据始终为空

ios – ASIHTTPRequest – 缓存中的数据始终为空,第1张

概述我正在我的iPhone / iPad应用程序中下载图像 – 因为它们在大多数时间保持不变,我想将它们缓存在磁盘上: ASIDownloadCache *cache = [ASIDownloadCache sharedCache];[asiRequest setDownloadCache:cache];[asiRequest setCacheStoragePolicy:ASICachePerma 我正在我的iPhone / iPad应用程序中下载图像 – 因为它们在大多数时间保持不变,我想将它们缓存在磁盘上:

ASIDownloadCache *cache = [ASIDownloadCache sharedCache];[asiRequest setDownloadCache:cache];[asiRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];[asiRequest setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];[asiRequest setSecondsToCache:60*60*24*30]; // Cache for 30 days[asiRequest setDelegate:self]; // A delegate must be specifIEd[asiRequest setDIDFinishSelector:@selector(requestFinished:)];[asiRequest startAsynchronous];

在我的requestFinished方法中,我们认识到缓存是成功的,但我的request-object中的数据是empyt;我错过了什么?

- (voID) requestFinished:(ASIhttpRequest *)request {   if ([request dIDUseCachedResponse]) {       NSLog(@"cache,size: %i",[[request responseData] length]); // is always zero       [self buildImageWithData:[request responseData]];   }   [...];}

非常感谢!

编辑1:我的完整代码无法正常工作:第一次,我得到“未缓存”语句,从那时起,“缓存,大小:0”……我不明白为什么:(

- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];    for (int i = 0; i < 10; i++) {        asiRequest = [[ASIhttpRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.myimageserver.com/myimage.jpg"]];        ASIDownloadCache *cache = [ASIDownloadCache sharedCache];        [asiRequest setDownloadCache:cache];        [asiRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];        [asiRequest setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];        [asiRequest setSecondsToCache:60*60*24*30]; // Cache for 30 days        [asiRequest setDelegate:self]; // A delegate must be specifIEd        [asiRequest setDIDFinishSelector:@selector(requestFinished:)];        [asiRequest startAsynchronous];    }}- (voID) request:(ASIhttpRequest *)request dIDReceiveResponseheaders:(NSDictionary *)responseheaders {    self.imageDataLoadedFromURL = [NSMutableData data];}- (voID) request:(ASIhttpRequest *)request dIDReceiveData:(NSData *)data {    [imageDataLoadedFromURL appendData:data];}- (voID) requestFinished:(ASIhttpRequest *)request {    if ([request dIDUseCachedResponse]) {        NSLog(@"cache,[[request responseData] length]); // is always zero    } else {        NSLog(@"not cached");    }}

编辑2:似乎缓存已经包含0字节图像;所以这不是从缓存中读取而是写入它的问题……

编辑3:您可以从http://tinyurl.com/68wuwj2下载小型测试项目

解决方法 你可以设置一个断点,清除缓存(例如删除你的应用程序),然后单步执行这段代码(ASIDownloadCache.m,第184行,方法storeResponseForRequest):

if ([request responseData]) {    [[request responseData] writetofile:dataPath atomically:NO];} else if ([request downloadDestinationPath] && ![[request downloadDestinationPath] isEqualToString:dataPath]) {    NSError *error = nil;    [[[[NSfileManager alloc] init] autorelease] copyItemAtPath:[request downloadDestinationPath] topath:dataPath error:&error];}

检查遵循的路径,并检查request.responseData的大小.

**编辑**

你的问题是你使用dIDReceiveData:

- (voID) request:(ASIhttpRequest *)request dIDReceiveData:(NSData *)data {    [imageDataLoadedFromURL appendData:data];}

引用asi http请求文档:

If you need to process the response as
it comes in (for example,you want to
use a streaming parser to parse the
response while it is still being
downloaded),have your delegate
implement request:dIDReceiveData: (see
ASIhttpRequestDelegate.h). Note that
when you do this,ASIhttpRequest will
not populate responseData or write the
response to downloadDestinationPath –
you must store the response yourself
if you need to.

最后一句似乎也适用于缓存数据.

它可能是asihttprequest中的一个错误,实际上它应该不是在这种情况下将数据写入缓存,或者应该确保它正在写入有效数据 – 目前看起来它写的是无效的缓存条目.你需要在数据到达时处理它吗?如果不是只删除’dIDReceiveBytes’并且只使用requestFinished中request.responseData中提供的数据.

@H_301_81@ 总结

以上是内存溢出为你收集整理的ios – ASIHTTPRequest – 缓存中的数据始终为空全部内容,希望文章能够帮你解决ios – ASIHTTPRequest – 缓存中的数据始终为空所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1051249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存