NSURLCache 在 iOS 4 中如何工作?
当我学习 NSURLCache 文档时,我找不到很多重要的注释。 我发现网络中的许多主题抱怨使用 NSURLCache 的 NSURLConnection 存在内存泄漏。当我尝试为 iOS 4.3 编写一些应用程序时,我发现 MSURLCache 有一些奇怪的行为。
在application:didFinishLaunchingWithOptions:中,我设置内存容量为10MB的NSURLCache实例,初始化 NSURLRequest 与 NSURLRequestUseProtocolCachePolicy 但我无法接收我的 NSURLConnection 委托中的 connection:willCacheResponse: 消息。当我检查应用程序不同点的内存容量时,它是 0MB。为什么? Apple 是否可以通过将 NSURLCache 内存容量设置为 0 来修复一些内存泄漏?我创建了自定义 NSURLCache 并通过以下方式重写了 setMemoryCapacity: 方法:
-(void)setMemoryCapacity:(NSUInteger)memoryCapacity {
if (memoryCapacity > 0) {
[super setMemoryCapacity:memoryCapacity];
}
else {
NSLog(@"zero here");
}
}
然后我开始调试。我的应用程序中有一些输入文本,用户在其中输入一些网址。新的 setMemoryCapacity: 方法已被调用两次:在我的应用程序委托中,当我将容量设置为 10MB 时,以及(非常有趣)当用户将焦点设置为文本输入时(参数内存容量 为 0,我在日志中收到“此处为零”字符串)。之后,我收到了发送给 NSURLConnection 委托的 connection:willCacheResponse: 消息。 这很奇怪。我不明白为什么。我在手册中找不到有关此的内容。你有什么想法吗?
When I learn NSURLCache documentation I can't find many important notes.
I found that many topics in web complain that NSURLConnection that use NSURLCache had memory leaks. When I tried to write some application for iOS 4.3 I found that MSURLCache have some strange behavior.
In application:didFinishLaunchingWithOptions: I set instance of NSURLCache with memory capacity 10MB, init NSURLRequest with NSURLRequestUseProtocolCachePolicy but I can't recieve connection:willCacheResponse: message in my NSURLConnection delegate. When I checked memory capacity in different points of my application, it was 0MB. Why? May be Apple fix some memory leaks by setting NSURLCache memory capacity to 0? I created custom NSURLCache and overrided setMemoryCapacity: method by this:
-(void)setMemoryCapacity:(NSUInteger)memoryCapacity {
if (memoryCapacity > 0) {
[super setMemoryCapacity:memoryCapacity];
}
else {
NSLog(@"zero here");
}
}
Then I started to debug. There is some input text in my app, where user put some url. New setMemoryCapacity: method has been called twice: in my app delegate, when I set capacity to 10MB and (it is very interesting) when user set focus to text input (parameter of memoryCapacity was 0 and I recieve "zero here" string in log). After that I recieved connection:willCacheResponse: messages to NSURLConnection delegate.
It was very strange. I can't understand why. I can't find things in manual about this. Do you have some thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论