对于想了解objective-c–NSURLConnection泄漏?的读者,本文将是一篇不可错过的文章,我们将详细介绍libcurl漏洞,并且为您提供关于ios–在Objective-C中的多个NS
对于想了解objective-c – NSURLConnection泄漏?的读者,本文将是一篇不可错过的文章,我们将详细介绍libcurl漏洞,并且为您提供关于ios – 在Objective-C中的多个NSURLConnection委托、ios – 将NSURLConnection切换到NSURLSession后,自定义NSURLProtocol变慢、ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript、ios – 用NSURLSession替换NSURLConnection的有价值信息。
本文目录一览:- objective-c – NSURLConnection泄漏?(libcurl漏洞)
- ios – 在Objective-C中的多个NSURLConnection委托
- ios – 将NSURLConnection切换到NSURLSession后,自定义NSURLProtocol变慢
- ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript
- ios – 用NSURLSession替换NSURLConnection
objective-c – NSURLConnection泄漏?(libcurl漏洞)
当我运行仪器,它说我有一个泄漏NSFNetwork对象.
并且如何释放(void)ButtonClicked中的连接?还是稍后再发布?
- (void)ButtonClicked { NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:KmlUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0f]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { // receivedData is declared as a method instance elsewhere NSMutableData *receivedData = [[NSMutableData data] retain]; [self setKMLdata:receivedData]; } else { // inform the user that the download Could not be made }}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // append the new data to the receivedData // receivedData is declared as a method instance elsewhere [KMLdata appendData:data]; NSLog(@"didReceiveData");}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // release the connection,and the data object [connection release]; [KMLdata release];}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ // release the connection,and the data object [connection release]; // receivedData is declared as a method instance elsewhere [KMLdata release];}
解决方法
上述代码中的错误(顺便说一下,来自SDK docs的近似精确的样本)不在内存管理代码中.自动释放是一种选择,手动释放是另一种选择.无论如何处理您的NSURLConnection对象,都会使用NSURLConnection获得泄漏.
首先,这里是解决方案.只需将这3行代码直接复制到connectionDidFinishLoading,didFailWithError和其他任何地方释放NSURLConnection对象.
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];[NSURLCache setSharedURLCache:sharedCache];[sharedCache release];
授予http://forums.macrumors.com/showthread.php?t=573253的mpramodjain代码.
问题似乎是这样的 – SDK缓存iPhone上的请求和回复.即使您的NSMutableuRLRequest cachePolicy设置为不从缓存加载回复似乎.
愚蠢的是,默认情况下它似乎缓存了很多数据.我传输了大量数据(分为多个连接),并开始获取内存警告,最后我的应用程序死了.
我们需要的文档是NSURLCache(不是NSURLConnection),它们说明:
NSURLCache implements the caching of
responses to URL load requests by
mapping NSURLRequest objects to
NSCachedURLResponse objects. It is a
composite of an in-memory and an
on-disk cache.Methods are provided to manipulate the
sizes of each of these caches as well
as to control the path on disk to use
for persistent storage of cache data.
这三行具有完全禁止缓存的效果.添加到我的应用程序(GPS Log)后,我的#living对象计数保持稳定.
ios – 在Objective-C中的多个NSURLConnection委托
我只是拿起Objective-C,我想知道什么是正确的方式来实现代表是.
现在我正在使用:
NSURL *url=[NSURL URLWithString:FeedURL]; NSURLRequest *urlR=[[[NSURLRequest alloc] initWithURL:url] autorelease]; NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:urlR delegate:self];
我不想使用self作为委托,如何定义与不同代表的两个连接?
NSURLConnection *c1 = [[NSURLConnection alloc] initWithRequest:url delegate:handle1]; NSURLConnection *c2 = [[NSURLConnection alloc] initWithRequest:url delegate:handle2];
我将如何创建handle1和handle2作为实现?还是接口?我真的不知道你会怎么做
任何帮助都是极好的.
谢谢,
Brian Gianforcaro
解决方法
DownloadDelegate *dd = [DownloadDelegate alloc];
这是危险的.代替:
DownloadDelegate *dd = [[DownloadDelegate alloc] init];
此外,在@interface声明中声明您的委托响应方法并不是绝对必要的(尽管它当然不会受到伤害).最后,您需要确保实现连接:didFailWithError:和connectionDidFinishLoading:释放您的DownloadDelegate对象,否则您将泄漏.
很高兴你开始运行!
ios – 将NSURLConnection切换到NSURLSession后,自定义NSURLProtocol变慢
我将使用NSURLConnection粘贴原始UrlProtocol,然后使用NSURLSession粘贴新类.原本的:
#import "UrlProtocol.h" #import "Globals.h" @implementation UrlProtocol + (BOOL)canInitWithRequest:(NSURLRequest *)request { if (![request.URL.absoluteString hasPrefix:@"http"]) return NO; //No need to intercept non-http requests if ([NSURLProtocol propertyForKey:@"handled" inRequest:request]) { return NO; } return YES; } + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { Nsstring* key = @"custom-auth-header"; Globals* globals = [Globals getInstance]; Nsstring* token = [globals token]; NSMutableuRLRequest *newRequest = [request mutablecopy]; //Create a mutable copy that can be modified [newRequest setValue:token forHTTPHeaderField:key]; return [newRequest copy]; //return a non-mutable copy } + (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b { return [super requestIsCacheEquivalent:a toRequest:b]; } - (void)startLoading { NSMutableuRLRequest *newRequest = [self.request mutablecopy]; [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest]; self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self]; } - (void)stopLoading { NSLog(@"stopLoading"); [self.connection cancel]; self.connection = nil; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.client URLProtocol:self didLoadData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self.client URLProtocolDidFinishLoading:self]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self.client URLProtocol:self didFailWithError:error]; } @end
针对每个请求使用NSURLSessionDataTasks的新UrlProtocol:
#import "UrlProtocol.h" #import "Globals.h" @implementation UrlProtocol + (BOOL)canInitWithRequest:(NSURLRequest * _Nonnull) request { if (![request.URL.absoluteString hasPrefix:@"http"]) return NO; //No need to intercept non-http requests if ([NSURLProtocol propertyForKey:@"handled" inRequest:request]) { return NO; } return YES; } + (NSURLRequest * _Nonnull)canonicalRequestForRequest:(NSURLRequest * _Nonnull)request { Nsstring* key = @"custom-auth-header"; Globals* globals = [Globals getInstance]; Nsstring* token = [globals token]; NSMutableuRLRequest *newRequest = [request mutablecopy]; //Create a mutable copy that can be modified [newRequest setValue:token forHTTPHeaderField:key]; return [newRequest copy]; //return a non-mutable copy } + (BOOL)requestIsCacheEquivalent:(NSURLRequest * _Nonnull)a toRequest:(NSURLRequest * _Nonnull)b { return [super requestIsCacheEquivalent:a toRequest:b]; } - (void)startLoading { NSMutableuRLRequest *newRequest = [self.request mutablecopy]; [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest]; [Globals setUrlSessionDelegate:self]; Globals* globals = [Globals getInstance]; self.dataTask = [globals.session dataTaskWithRequest:newRequest]; [self.dataTask resume]; } - (void)URLSession:(NSURLSession * _Nonnull)session dataTask:(NSURLSessionDataTask * _Nullable)dataTask didReceiveData:(NSData * _Nullable)data{ [self.client URLProtocol:self didLoadData:data]; } - (void)URLSession:(NSURLSession * _Nonnull)session dataTask:(NSURLSessionDataTask * _Nullable)dataTask didReceiveResponse:(NSURLResponse * _Nullable)response completionHandler:(void (^ _Nullable)(NSURLSessionResponsedisposition))completionHandler{ [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; completionHandler(NSURLSessionResponseAllow); } - (void)URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task didCompleteWithError:(NSError * _Nullable)error{ if (error){ [self.client URLProtocol:self didFailWithError:error]; } else { [self.client URLProtocolDidFinishLoading:self]; } } - (void)URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task willPerformHTTPRedirection:(NSHTTPURLResponse * _Nonnull)response newRequest:(NSURLRequest * _Nonnull)request completionHandler:(void (^ _Nonnull)(NSURLRequest * _Nullable))completionHandler { completionHandler(request); } - (void)stopLoading { [self.dataTask cancel]; self.dataTask = nil; } @end
“Globals”是一个单例,我已经初始化了一个NSURLSession,意在整个应用程序的运行时使用.它还包含我为所有请求设置为自定义HTTP标头的令牌:
#import "Globals.h" #import "UrlProtocol.h" @implementation Globals @synthesize token; @synthesize session; static Globals *instance = nil; +(Globals*) getInstance { @synchronized(self) { if (instance == nil) { instance = [Globals new]; } } return instance; } //UrlProtocol class has no init method,so the NSURLSession delegate is being set on url load. We will ensure only one NSURLSession is created. +(void) setUrlSessionDelegate:(UrlProtocol*) urlProtocol{ Globals* globals = [Globals getInstance]; if (!globals.session){ globals.session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration delegate:urlProtocol delegateQueue:nil]; } } @end
解决方法
- (void)startLoading { NSMutableuRLRequest *newRequest = [self.request mutablecopy]; [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest]; NSURLSessionConfiguration* config = NSURLSessionConfiguration.defaultSessionConfiguration; NSURLSession* session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; self.dataTask = [session dataTaskWithRequest:newRequest]; [self.dataTask resume]; }
之前的简单HTML页面测试必须有效,因为加载页面只需要一个任务
ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript
我发现它的概念非常有趣.它似乎主要是语法糖,看起来很可读.
虽然它不直接编译到Objective-C,但它声称生成与Objective-C相同的二进制代码
Eero compiles down to the same binary code as Objective-C
Eero offers excellent,nearly seamless interoperability with
Objective-C,C,and C++.
我发现这种方法非常有趣,我想知道是否有类似的编程语言和项目提供与Objective-C和iOS的大腿集成.
我正在为Objective-C寻找类似Coffeescript的东西.
解决方法
它确实需要记录在一个更明显的地方……
ios – 用NSURLSession替换NSURLConnection
网络管理器实例化NSOperation子类(它封装了URL,参数等),并将其添加到它维护的NSOperationQueue中.
NSOperation子类实例化NSURLConnection(执行异步请求并检索数据)
NSURLConnection将数据转储到NSOperation-subclass
NSOperation-subclass执行由delegate(视图控制器)提供的完成块.
我现在试图用NSURLSession实现相同的模式.我希望能够封装将单个对象中的网络请求所需的url和参数.
如果我使用相同的模式,如果我使用NSURLSession,它是否正常.我检查了AFNetworking课程.但是,他们没有将NSOperation用于NSURLSession.而且,会话对象应该在哪里.它会是NSOperation类的一部分.
有人可以给出一些专家意见.我应该能够取消请求,上传(POST / PUT),下载数据.网络管理器类将是任何网络请求的单点联系.
解决方法
NSURLConnection不利于Mac OS 10.11和iOS 9.所以,此时NSURLSession应该用来代替NSURLConnection.正如NSURLConnection.h头所示:
Deprecated: The
NSURLConnection
class should no longer be used.NSURLSession
is the replacement forNSURLConnection
.
我原来的答案在下面.
答案取决于您是否需要各种NSURLSession代表方法的丰富性.如果您使用完成块转换(即没有进程回调,无流式传输等),则从NSURLConnection到NSURLSession的转换很简单.只需将您的NSURLSession实例放在NetworkManager类中,然后在一个并发的NSOperation子类中包装基于NSURLSessionTask实例的委托,你就完成了.只需采用标准异步/并发NSOperation子类型.
如果您使用基于委托的NSURLSession转载,它是一个完全不同的水壶.主要的麻烦是,在会话委托而不是任务委托对象上调用了各种NSURLSessionTask委托方法.首先腮红,这可能听起来像是一个微不足道的问题,但是如果您的操作实例具有唯一的完成/进度块,那么您就会遇到如何让会话对象将这些委托方法回调映射到个人的麻烦原始请求操作实例.
要解决这个问题,您必须将任务标识符映射到NSOperation子类对象.然后可以在各自的NSOperation子类中实现这些NSURLSessionTask(包括任务,下载任务和上传任务)委托方法. NSURLSession网络管理器类可以在收到NSURLSessionTask委托调用时使用任务标识符来标识适当的NSOperation实例,然后在那里调用适当的委托方法.
最后,如果您打算处理背景NSURLSession实例,生活将变得更加困难(因为即使在应用程序终止并且所有对象已被丢弃之后,后台任务也将继续).背景课程根本不适用于基于NSOperation的方法.
底线,如果您只需要完成块NSURLSession方法,这是微不足道的,但如果您需要基于代理的再现,这是一个麻烦.
关于objective-c – NSURLConnection泄漏?和libcurl漏洞的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ios – 在Objective-C中的多个NSURLConnection委托、ios – 将NSURLConnection切换到NSURLSession后,自定义NSURLProtocol变慢、ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript、ios – 用NSURLSession替换NSURLConnection等相关知识的信息别忘了在本站进行查找喔。
本文标签: