Caching on AFNetworking 2.0

前端 未结 1 1505
渐次进展
渐次进展 2021-01-06 22:09

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

NSMutableURLRequest* rq = [api requestW         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 22:13

    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.

    0 讨论(0)
提交回复
热议问题