- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想从我的应用程序中的多个位置访问 NSCache,因为我正在使用它来缓存来自 API 端点的图像。
例如下图中的 TableView 4 和 View Controller 6 使用相同的图像,所以我不想下载它们两次。
候选解:
单例
class Cache {
private static var sharedCache: NSCache<AnyObject, AnyObject>?
static public func getCache () -> NSCache<AnyObject, AnyObject> {
if sharedCache == nil {
self.sharedCache = NSCache()
}
return sharedCache!
}
}
似乎工作正常,但“单例很糟糕”所以......
这会将 View 与 View Controller 紧密耦合,因此...
以某种方式存储在 AppDelegate 中。但这不是和1一样吗?所以……
使用依赖注入(inject)。但是我们在选项卡 View Controller 中,所以这不是和 2 一样吗?
我不确定这里的策略是否正确,所以我想问是否有其他方法可以在这里使用。
我做了什么 使用 NSCache 创建了一个带有示例的应用程序,并探索了单例解决方案。我试过使用依赖注入(inject),但认为它没有意义。我查看了 Stack overflow 和文档,但对于这种特定情况,我没有发现任何潜在的解决方案。
我给出的内容一个最小的示例,其中包含我不满意的图表和经过测试的解决方案。
没有帮助的是说 NSCache 不正确或使用库的答案。我正在尝试使用 NSCache 进行我自己的学习,这不是家庭作业,我想解决此 App 结构中此问题的具体实例。
问题是什么如何避免在这种情况下使用单例,在选项卡 View Controller 中查看 Controller 。
最佳答案
首先。单例并不是天生就是坏的。它们会使您的代码难以测试,并且它们确实起到了依赖性磁铁的作用。
单例适用于作为工具的类,例如 NSFileManager
又名 FileManger
,即不携带状态或数据的东西。
一个很好的替代方法是依赖注入(inject),但是对于 View Controller 和 Storyboard,它可能很难并且感觉很像样板。您最终在 prepareForSegue
中传递了所有内容。
一种可能的方法是声明一个协议(protocol)
来描述一个类似缓存的接口(interface)。
protocol CacheProtocol: class {
func doCacheThing()
}
class Cache: CacheProtocol {
func doCacheThing() {
//
}
}
然后声明一个协议(protocol)
,所有希望使用这个缓存的东西都可以使用。
protocol CacheConsumer: class {
var cache: CacheProtocol? { get set }
func injectCache(to object: AnyObject)
}
extension CacheConsumer {
func injectCache(to object: AnyObject) {
if let consumer = object as? CacheConsumer {
consumer.cache = cache
}
}
}
最后在顶层创建这个缓存的具体实例。
/// Top most controller
class RootLevelViewController: UIViewController, CacheConsumer {
var cache: CacheProtocol? = Cache()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
injectCache(to: segue.destination)
}
}
您可以在 prepareForSegue
中传递缓存。
或者您可以使用微妙的子类化来创建一致性。
class MyTabBarController: UITabBarController, CacheConsumer {
var cache: CacheProtocol?
}
或者你可以使用委托(delegate)方法让缓存对象广播下来。
extension RootLevelViewController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
injectCache(to: viewController)
}
}
您现在拥有一个系统,其中任何 CacheConsumer
都可以使用缓存并将其向下传递给任何其他对象。
关于swift - 在选项卡 View Controller 中跨 View Controller 访问 NSCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55470792/
我有 NSCache,我在其中加载和加载要在 Collection View 中显示的图像。当我必须将数据重新加载到 Collection View 时,我必须清理缓存,否则 Collection V
对于我的应用程序,我必须将一些 HTML 转换为 tableViews 中的 NSAttributedStrings。这是一个 CPU 密集型操作,所以我决定利用 NSCache 有效地将时间减半,因
当用户通过菜单在 View Controller 之间切换时,如果他有足够的内存,我想缓存它们。为此,我想使用 NSCache 来存储许多 UIViewControllers。 但是,我不确定如何告诉
我有一个 collectionView,它一次显示 9 张图像(200x300 像素)。不过,总数可能高达数百。 目前,如果图像在缓存中,我会检查 cellForItem。如果没有,我将从文件系统加载
我正在编写一个需要在缓存中存储一些图像的应用程序。我正在尝试使用 NSCache 来完成它,代码似乎很好,但不要将图像保存在缓存中。我有这段代码: 缓存是全局的,在.h中声明:NSCache *c
我目前使用 NSCache 的一个子类来存储一些图像(值)及其相应的名称(键),当应用程序位于前台时,它似乎工作正常。但是,当我按下主页按钮/用户进入后台并重新进入应用程序时,NSCache 是空的。
我正在研究一种相当简单的缓存机制,目前正在使用 NSCache(出于内存管理的原因)。问题是——缓存是否在两次启动之间持续存在?对我来说好像不行。 我可以很好地将对象放入缓存中,并且可以在应用程序运行
我已经实现了一个列表,其中异步加载了许多大图像。我正在使用 NSCache 的实例来存储这些 UIImage 文件。当我向下滚动时,我加载了越来越多的图像。我设置了它并分析了应用程序的分配情况,我注意
我的应用程序执行某些项目的搜索,并根据获得的每个结果下载图像。它在 tableView 上显示结果,每个结果一个单元格,每个单元格显示相应的图像。 如果已经使用相同的参数进行搜索,我想缓存图像以避免下
我目前使用 NSCache 的子类来存储一些图像(值)及其相应的名称(键),当应用程序位于前台时,它似乎工作正常。但是,当我按下主页按钮/用户进入后台并重新进入应用程序时,NSCache 是空的。 实
NSCache 的一些自动删除策略是什么? Apple的文档没有提到它们,我实验发现NSCache没有响应内存警告。 最佳答案 NSCache 不响应 UIApplicationDidReceiveM
NSCache是一个很少使用的工具,实际上看起来很有用。我创建了一个简单的实验来查看它是如何工作的,看起来它不会在内存不足的情况下自动逐出数据(或者我做错了什么!) - (void)viewDidLo
我注意到当应用程序进入后台时,NSCache 会逐出它的所有对象。那是预期的行为吗?有办法避免吗? 我希望它在设备内存不足时驱逐对象,而不是在应用进入后台时立即驱逐对象。 你知道任何有效的替代方案吗?
谁能举例说明如何使用 NSCache 缓存字符串?或者任何人都有一个很好的解释的链接?好像没找到啊。。 最佳答案 您使用它的方式与使用 NSMutableDictionary 的方式相同。不同之处在于
我正在使用 NSCache 来存储 webData。 webData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:urlStr
我正在编写一个应用程序,需要保留一堆对象的内存缓存,但这不会失控,因此我计划使用 NSCache 来存储所有内容。看起来它会负责清理等工作,这对我来说太棒了。 我还想在启动之间保留缓存,因此我需要将缓
由于 NSCache 已更改为泛型类型 NSCache 现在,这就是我正在执行的代码片段但是,控制台正在打印 -“图像到缓存后”并调度主队列 CustomImageView,但执行的缓存中的图像没有显
我有一个 NSCache 的实例,例如:NSCache *imageCache; 它基本上用于保存一些具有不同 "@keys 的远程“图像”值“。我在开头的全局类中 alloc & init NSCa
我这辈子都想不出如何在 NSCache 中存储与可选项有关的任何内容。我可以理解直接存储一个可选的是没有意义的,但是存储一个数组或字典,其中一些选项可能是可选的,这似乎应该是可能的。 然而,一个简单的
这应该在我的任何给定表格行中显示一个占位符,除非存在该索引路径的图像。当我向下滚动时,先前 indexPath 中的图像会加载默认值而不是释放。我知道之前有人帮助我解决了同样的问题,但这是在我意识到缓
我是一名优秀的程序员,十分优秀!