`
zhy584520
  • 浏览: 183792 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

AFNetWorking and SDUrlCache 缓存http请求

    博客分类:
  • IOS
 
阅读更多
     SDURLCache *urlCache = [[SDURLCache alloc]
     initWithMemoryCapacity:1024*1024*2   // 2MB mem cache
     diskCapacity:1024*1024*15 // 15MB disk cache
     diskPath:[SDURLCache defaultCachePath]];
     [urlCache setMinCacheInterval:1];
     [NSURLCache setSharedURLCache:urlCache];
     
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"从服务端获取数据:%@",operation.responseString);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     if ([urlCache isCached:[NSURL URLWithString:@"http://www.baidu.com"]]) {
     
     }
     NSCachedURLResponse *resp = [urlCache cachedResponseForRequest:request];
     NSString  *str = [[NSString alloc] initWithData:resp.data encoding:NSUTF8StringEncoding];
     NSLog(@"从缓存中获取数据:%@",str);
     }];
     [operation start];

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics