- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的应用程序中有几个内存泄漏(不!请参阅更新 1),它们都归结为异步 URLRequest。下面的代码给了我一个内存泄漏,似乎“数据”从未被释放(下面的代码在逻辑上没有在我的应用程序中使用,因为它是一个完全无用的无限循环,我只是写它来显示内存泄漏。这个使使用的 RAM 在不到一秒内从 5 MB 增加到 20 MB。我的网速确实符合这个 [仅作记录]):
- (void)start{
NSOperationQueue *oQC = [[NSOperationQueue alloc] init];
NSLog(@"a");
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.online-image-editor.com//styles/2014/images/example_image.png"]] queue:oQC completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"b");
[self start];
}];
}
我也试过设置 data = nil,但是,就像假设的那样,不起作用。是否有人知道如何避免内存泄漏以及这是否是 NSURLConnection 的正常行为?
更新 1:
这似乎与内存泄漏无关,而是与缓存问题有关。 (感谢@rdelmar,他看到了问题,但他的解决方案并不奏效)
基于 this post我尝试在它的 .h 文件中创建一个新的“Loader”类:
#import <Foundation/Foundation.h>
@interface Loader : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property NSMutableData *mData;
@property (nonatomic, copy) void (^returnBlock)(NSData *data, NSError *error);
- (id)initForWhateverWithURLString:(NSString*)urlString andHandler:(void (^)(NSData *data, NSError *error))handler;
@end
这是 .m 文件中的内容:
#import "Loader.h"
@implementation Loader
@synthesize mData;
- (id)initForWhateverWithURLString:(NSString*)urlString andHandler:(void (^)(NSData *data, NSError *error))handler{
self = [super init];
if (self) {
/*NSOperationQueue *oQC = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:mUR queue:oQC completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:mUR];
handler(nil, nil);
}];*/
mData = [NSMutableData new];
self.returnBlock = handler;
NSMutableURLRequest *mUR = [[NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60] mutableCopy];
NSURLConnection *URLCon = [[NSURLConnection alloc] initWithRequest:mUR delegate:self];
[URLCon start];
}
return self;
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[mData appendData:data];
data = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
self.returnBlock(mData, nil);
mData = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
self.returnBlock(nil, error);
}
@end
我还实现了:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
尽管如此,这些方法都无法帮助减少急剧的 RAM 使用量!
最佳答案
我不认为你看到的是泄漏。我认为内存不断增加,因为正在缓存数据。如果您更改请求的缓存协议(protocol),问题就会消失(您将获得默认行为,NSURLRequestUseProtocolCachePolicy,通过使用 requestWithURL:)
- (void)viewDidLoad {
[super viewDidLoad];
self.req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.online-image-editor.com//styles/2014/images/example_image.png"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
[self start];
}
- (void)start{
NSOperationQueue *oQC = [NSOperationQueue mainQueue]; // this queue is where the completion block runs, so you should use mainQueue if you want to do any UI updating
NSLog(@"a");
[NSURLConnection sendAsynchronousRequest:self.req queue:oQC completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"b");
[self start];
}];
}
编辑后:
在进一步查看之后,我不确定为什么 sendAsynchronousRequest: 会导致内存使用量随时间增加。我使用 NSURLSession(无论如何我们现在应该使用它)进行了测试,这似乎没有导致内存增加。
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlString = @"http://www.online-image-editor.com//styles/2014/images/example_image.png";
self.req = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
[self start];
}
- (void)start{
NSLog(@"a");
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:self.req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.counter ++;
NSLog(@"counter is: %ld", self.counter);
[self start];
}];
[task resume];
}
关于iOS NSURLConnection (sendAsynchronousRequest :) too much cache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678865/
我正在寻找有关使用 NSURLConnection 异步请求的好教程。我一直在 stackoverflow 和谷歌中四处寻找,但找不到。这可能是无数类似此处问题的重复。但请指导我正确的教程,我以前使用
我在植入简单的 NSURLConnection 时遇到了这个奇怪的问题... 调用了 didReceiveData 方法,我很乐意尝试附加接收数据,但是... nada! 肯定有一些数据(如长度所示,
我是 iOS 互联网连接的新手。 我正在尝试从特殊站点获取数据。 以下代码适用于所有站点以查看 url 中的数据。 但是,如果我将其更改为特殊站点以获取他们的数据,它将返回 NULL! 我认为该站点如
在 iOS 9 中,我将 NSURL 与 NSURLConnection 一起使用,对于一个特定的 URL,它在 - (void)connection:(NSURLConnection *)conne
多个 NSURLConnections 正在启动(在单个 UIViewController 中)以收集不同类型的数据。当他们返回(-connectionDidFinishLoading)时,我想根据到
谁能帮助我理解这些问题:使用 Alamofire 优于 NSURLSession/NSURLConnection 有什么优势? NSURLSession 和 NSURLConnection 有什么区别
是否可以取消 NSURLConnection开始于 sendAsynchronousRequest:queue:completionHandler: ? 为什么不sendAsynchronousReq
你好, 我正在试验一个非常恶性的崩溃,它在设备日志中以下列方式显示: Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_I
这是一个开放性问题,旨在了解什么是我认为可能很常见的问题的最佳实践或最常见的解决方案。 假设我有一个要下载的 URL 列表;该列表本身托管在服务器上,因此我启动了一个 NSURLConnection
我基本上将RestKit与RKClient一起使用,并执行get请求。我的系统使用用户名/密码/帐户来认证用户。它只是用于用户名/密码的httpbasic,而帐户只是一个子域。 我的问题是,如果执行以
我尝试使用 [sendSynchronousRequest: returnedResponse: error:] 代码连接服务器,但证书未经验证,导致出现错误。我已经读过 How to use NSU
我正在使用 NSURLConnection 通过 GET 请求与服务器连接。要登录,我将发送带有表单 url 的 GET 请求 http://:@my.serverurl.com/user/login
我正在将图像上传到我的服务器,当我上传图像时,我试图向用户显示上传进度条。但在 NSURLConnection 委托(delegate)方法 连接中: didSendBodyData:totalByt
这是另一个简单的问题,我在 Google 上搜索了一个小时后却找不到答案... 用户正在文本字段中输入内容,当该字段失去焦点时,我启动 NSURLConnection.... NSString *us
我正在尝试验证连接是否成功,但得到的结果不稳定。当我尝试使用虚假网址执行同步请求时: NSData *responseData = [NSURLConnection sendSynchronousRe
我正在将图像加载到 iPhone 上的图库中,但遇到了问题。显然,当尝试从互联网下载图像时,脚本中的某些内容对文件名中的空格不满意。 这是连接线。 NSURLConnection *conn = [[
我有一个 NSURLConnection,每次调用 -(void)viewWillAppear:animated 时都会调用它(仅用于测试) 我就是这样做的 receivedData = [[NSMu
在 NSURLConnection 上调用实例方法 cancel 通常根本不会取消连接。 我正在 NSOperation 中执行异步 NSURLConnection。如果取消操作,则通过调用 NSUR
当我允许加载完成时,我有一个 NSURLConnection 工作正常。但是如果用户点击后退按钮,意味着 webview 将消失,我想取消正在进行的 NSURLConnection。但是,如果在调用
我的客户端应用程序中有一个使用 NSURLConnection 的小错误。我已经追踪到一个意外的连接保持事件,这似乎使网络服务器感到困惑(可能是服务器端的错误)。解决方法是在某个时刻强制关闭所有未完成
我是一名优秀的程序员,十分优秀!