iOS - 本地保存图像的最佳实践 - NSCache vs保存在文档目录中 [英] iOS - Best practice to save Images locally - NSCache vs Save in Document Directory

查看:231
本文介绍了iOS - 本地保存图像的最佳实践 - NSCache vs保存在文档目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个类似于Instagram Feed的应用程序(包含图像和一些标签的单元格的表格视图)。

I'm developing an app that similar to Instagram feed (tableviews with cells that contain images and some labels).

对于我从中获取的所有数据数据库,我正在使用数据任务(因为它不需要太多接收它们),但是对于图像(我用数据请求获取它们的URL),我需要在本地保存以备将来使用(改善用户体验) )。

For all the data I'm getting from the database, I'm using Data Task (because it doesn't take much to receive them), but for the images (which their url's I get with the Data request), I need to save locally for future use (improve user experience).

我的逻辑如下:
保存在NSCache或文档目录中,文件夹中的图像及其下载日期(创建一次,如果需要,附加所有其他图像)(我正在删除不是最近7天的每个文件夹),然后对于TableView,只需从那里加载,因此tableview将平滑滚动并且不会直接从其委托方法加载网址。
那么哪里有更好的地方可以根据我的需要存储它们, NSCache或文档目录

My logic is the following: Save in NSCache or in Document Directory, the images inside folder with the date they been downloaded(create it once and append all other images if needed) (I'm deleting every folder which is not from the recent 7 days), and then for the TableView, just load if from there, so the tableview will scroll smoothly and won't load the url directly from its delegate method. So where is the better place to store them according to my needs, NSCache or Document Directory.

期待听到你的声音建议,谢谢!

Looking forward to hearing your suggestions, Thank you!

推荐答案

NSCache 和持久存储服务差异很大目的。 NSCache 将项目保存在内存中,用于获得最佳性能。但它占用内存(RAM),你真的应该确保如果你使用 NSCache 你响应内存警告并清除 NSCache 在这些情况下。当应用程序终止时, NSCache 将丢失。

NSCache and persistent storage serve largely different purposes. NSCache holds the item in memory and is used for optimal performance. But it takes up memory (RAM) and you really should make sure that if you use NSCache that you respond to memory warnings and purge the NSCache in those cases. And when the app terminates, the NSCache is lost.

使用持久存储缓存(通常为 Caches 文件夹)用于不同的目的,使您无需通过某些网络请求重新检索资产,但不会将资源保留在内存中。这使它成为运行应用程序的会话之间的一个很好的缓存机制,或者在你可能遇到内存压力的情况下,清除 NSCache ,但不想重新检索来自网络的资产。

Using persistent storage cache (generally the Caches folder) is used for a different purpose, saving you from needing to re-retrieve the asset via some network request, but not holding the resource in memory. This makes it a great cache mechanism across sessions of running the app or in situations where you may have encountered memory pressure, purged the NSCache, but didn't want to re-retrieve the asset from the network.

请注意,我提到持久存储的 Caches 文件夹,而您似乎在假设那个人会使用 Documents 文件夹,但有两个注意事项:

Note that I mention the Caches folder for persistent storage, whereas you seemed to presume that one would use Documents folder, but there are two considerations:


  1. Apple正在使用 Documents 文件夹来更加特别关注无法轻松重新创建的用户数据,并使用缓存容易重新检索的数据文件夹。有关更多信息,请参阅文件系统基础知识信息。

  1. Apple is getting more particular about apps only using Documents folder for user data that cannot be easily recreated, and using Caches folder for data that is easily re-retrieved. See File System Basics for more information.

从iOS 11开始,您应该只将用户可见文档存储在 Documents 文件夹中(参见WWDC) 2017秋季视频, iOS存储最佳实践)。即使您在内部使用了不易重建的文件,除非意图最终将用户暴露给他们,否则您将使用 Application Support 目录,而不是凭证文件夹。

Starting with iOS 11, you should only store user visible documents in the Documents folder (see WWDC 2017 Fall video, iOS Storage Best Practices). Even if you had internally used files that were not easily reconstructed, unless the intent was to eventually expose the user to them, you'd use the Application Support directory, not the Documents folder.

底线,一般会使用缓存基于持久存储的缓存的文件夹。

Bottom line, one would generally use the Caches folder for a persistent storage based cache.

注意,我们经常使用双层缓存机制。将资源缓存到 <$ em $ c> c> NSCache 和缓存文件夹。然后,当您去检索资源时,首先检查 NSCache (非常快),如果不存在,请检查持久存储,如果不存在,则重新检索资产网络。

Note, we'll often use a two-tier cache mechanism. Cache the resource to both NSCache and the Caches folder. Then, when you go to retrieve a resource, first check NSCache (really fast), if not there, check persistent storage, and if not there, re-retrieve the asset from the network.

说了这么多,为了使它变得更复杂,还有第三种类型的缓存,由 NSURLCache <提供/ code>(即网络请求的响应由 NSURLSession NSURLConnection 透明地缓存)。此缓存由记录不良的规则决定(例如,它不会缓存大小超过总缓存大小的5%的任何单个项目),并受网络响应提供的HTTP标头的约束。但是,此缓存对您来说非常透明,并提供内存和持久存储缓存。通常,您可以享受 NSURLCache 缓存行为,而您绝对不会干预。它是无缝的(当它工作时)。

Having said all of that, to make it even more complicated, there is a third type of cache, that provided by NSURLCache (i.e. responses for network requests are transparently cached by NSURLSession and NSURLConnection). This cache is dictated by poorly documented rules (e.g. it won't cache any single item whose size exceeds 5% of the total cache size) and is subject to the HTTP headers provided by the network response. This cache, though, operates largely transparently to you and provides both memory and persistent storage caches. Often you can enjoy NSURLCache caching behavior with absolutely no intervention on your part. It's seamless (when it works).

这篇关于iOS - 本地保存图像的最佳实践 - NSCache vs保存在文档目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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