在AFNetworking 2.0上缓存 [英] Caching on AFNetworking 2.0

查看:87
本文介绍了在AFNetworking 2.0上缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里的交易。我最近开始使用AFNetworking使用以下代码开始下载几个文件:

  NSMutableURLRequest * rq = [api requestWithMethod:@ GETpath:@YOUR / URL / TO / FILEparameters:nil]; 
AFHTTPRequestOperation * operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];

NSString * path = [@/ PATH / TO / APPstringByAppendingPathComponent:imageNameToDisk];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation,id responseObject){

NSLog(@SUCCCESSFULL IMG RETRIEVE to%@!,path)

}失败:^(AFHTTPRequestOperation * operation,NSError * error){

//处理失败
}];

我的路径实际上已插入路径变量(对不起,不是在正确的计算机上复制面食的文本,但它是完全相同的上述与不同的路径东西)



一切都很好!我得到文件成功下载和一切。我当前的问题是,我试图让缓存工作,但我有很多困难。基本上,我不知道我实际上有什么要客户端作为AFNetworking 2.0。我还需要设置NSURlCache吗?我需要在请求操作上不同地设置缓存类型头吗?我认为也许只是完全内置,但我收到一个状态为200每次代码运行,即使没有更改的文件。如果我必须使用NSUrlCache,我必须手动保存电子标签成功阻止requestoperation自己,然后反馈回来?任何帮助如何进步将非常感激。

解决方案

AFNetworking默认使用NSURLCache进行缓存。从常见问题



< blockquote>

AFNetworking利用已经由 NSURLCache 及其任何子类提供的缓存功能。只要您的 NSURLRequest 对象具有正确的缓存策略,并且您的服务器响应包含有效的 Cache-Control 头,


请注意,此机制会缓存 NSData ,因此每次从此缓存中检索时,都需要执行一些有点昂贵的 NSData -to - UIImage 操作。这不足以快速显示,例如,如果你在 UITableView UICollectionView 中显示图像。 / p>

如果是这种情况,请查看 UIImageView + AFNetworking ,它会将 UIImage 对象的下载和缓存添加到 UIImageView 。对于一些应用程序,您可以使用开箱即用的实现,但它是非常基本的。你可能想看看这个类的源代码(它不是很长),并使用它作为您自己的缓存机制的起点。


So here's the deal. I recently started using AFNetworking to download a few files on start using the following code:

NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE"     parameters:nil];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease];

NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path)

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // Deal with failure
}];

With my path actually plugged into the path variable (Sorry, not on the right computer right now to actually copy pasta the text, but it's exactly the same as the above with different path stuffs)

And everything is working great! I'm getting the file successfully downloaded and everything. My current issue is that I'm trying to get caching to work, but I'm having a lot of difficulties. Basically, I'm not sure what I actually have to do client side as of AFNetworking 2.0. Do I still need to set up the NSURlCache? Do I need to set the caching type header on the request operation differently? I thought that maybe it was just entirely built in, but I'm receiving a status of 200 every time the code runs, even with no changes in the file. If I do have to use the NSUrlCache, do I have to manually save the e-tag on the success blocks requestoperation myself and then feed that back in? Any help on how to progress would be much appreciated. Thanks guys!

解决方案

AFNetworking uses NSURLCache for caching by default. From the FAQ:

AFNetworking takes advantage of the caching functionality already provided by NSURLCache and any of its subclasses. So long as your NSURLRequest objects have the correct cache policy, and your server response contains a valid Cache-Control header, responses will be automatically cached for subsequent requests.

Note that this mechanism caches NSData, so every time you retrieve from this cache you need to perform a somewhat expensive NSData-to-UIImage operation. This is not performant enough for rapid display, for example if you're showing images in a UITableView or UICollectionView.

If this is the case, look at UIImageView+AFNetworking, which adds downloads and caching of UIImage objects to UIImageView. For some applications you can just use the out-of-the-box implementation, but it is very basic. You may want to look at the source code for this class (it's not very long) and use it as a starting point for your own caching mechanism.

这篇关于在AFNetworking 2.0上缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆