使用ASIHTTPRequest和ASIDownloadCache实现本地缓存

  源码:https://files.cnblogs.com/ios8/ASIHttpRequestDemo2.zip

NSURL *url = [NSURLURLWithString:@"http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg"];

 ASIFormDataRequest *request = [ASIFormDataRequestrequestWithURL:url];//创建数据请求对象

    [request setRequestMethod:@"GET"];

    [request setTimeOutSeconds:60];

//    [request setDelegate:self];//设置代理

    

  

   

//    设置缓存--

     ASIDownloadCache *cache = [[ASIDownloadCachealloc]init];//创建缓存对象

    NSString *cachePath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]; //设置缓存目录,这里设置沙盒目录下的Documents目录作为缓存目录

    NSLog(@"cachepath:%@",cachePath);

    [cache setStoragePath:cachePath];

    cache.defaultCachePolicy =ASIOnlyLoadIfNotCachedCachePolicy//设置缓存策略

    

    //每次请求会将上一次的请求缓存文件清除

    //    request.cacheStoragePolicy = ASICacheForSessionDurationCacheStoragePolicy;

    //持久缓存,一直保存在本地(是持久缓存,程序下次启动,缓存仍然还在)

    request.cacheStoragePolicy =ASICachePermanentlyCacheStoragePolicy;

    request.downloadCache = cache;

    

   

    

    [request startAsynchronous];//发送异步请求

    

    //设置网络请求完成后调用的block

    [request setCompletionBlock:^{

        

        //         NSLog(@"%@",request.responseHeaders);

        

        NSData *data = request.responseData;

        self.showImageView.image = [UIImageimageWithData:data];

        

        //---------------判断数据的来源:网络 or缓存------------------

        if (request.didUseCachedResponse) {

            NSLog(@"数据来自缓存");

        } else {

            NSLog(@"数据来自网络");

        }

        

    }];

    

    //请求失败调用的block

    [request setFailedBlock:^{

        NSError *error = request.error;

        NSLog(@"请求网络出错:%@",error);

    }];

    

 

 



在 NSData *data = request.responseData;这一行报警告:::Capturing \'request\'

 

 strongly in this block is likely to lead to a retain cycle.......... 

 



先开始我想应该是内存管理的问题,后来想自己使用了arc应该不存在这个问题吧。。后

来朋友说,block里面不能使用自己自定义的属性,因为block是独立的。。网上查了一下

关于block的使用问题,看的懵懵懂懂不太明白,没有理解。。。。没有明白。。这个和

我的程序死掉有什么关系??我写的代码,

点击打开链接这个网站的写的......




嗯,现在我把设置缓存的代码注释掉了,程序就可以了。。。这个跟缓存有关么?

 
------------------------------------------------------------------------
 
---------------------------------------
 
点击打开链接   使用ASIHTTPRequest和ASIDownloadCache实现本地缓存
 
 
好啦,这个问题是设置缓存的问题。。

ViewController.m

 

@interface ViewController (){

 

    ASIDownloadCache *myCache; 

}

 
修改缓存的代码:
 

//    设置缓存--

     ASIDownloadCache *cache = [[ASIDownloadCachealloc]init];//创建缓存对象

     myCache = cache;

    

   //路径

     NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    

   NSString *documentDirectory = [pathsobjectAtIndex:0];

    NSLog(@"path-%@",documentDirectory);

    

    //设置缓存存放路径

    

    [myCachesetStoragePath:[documentDirectorystringByAppendingPathComponent:@"pic"]];

    

    myCache.defaultCachePolicy =ASIOnlyLoadIfNotCachedCachePolicy//设置缓存策略

    

    //每次请求会将上一次的请求缓存文件清除

    //    request.cacheStoragePolicy = ASICacheForSessionDurationCacheStoragePolicy;

    //持久缓存,一直保存在本地(是持久缓存,程序下次启动,缓存仍然还在)

    request.cacheStoragePolicy =ASICachePermanentlyCacheStoragePolicy;

    request.downloadCache = cache;

 

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

 

打印了路径,前往文件夹,输入路径,就的到以下图示:

 

posted @ 2013-10-17 10:46  苹果吧  阅读(1232)  评论(1编辑  收藏  举报