Ios 无法追踪的网络内存泄漏
TL;DR:自己克隆并检查泄漏 我有一个处理所有网络的类。它被称为Ios 无法追踪的网络内存泄漏,ios,objective-c,memory-leaks,afnetworking,nsurlsession,Ios,Objective C,Memory Leaks,Afnetworking,Nsurlsession,TL;DR:自己克隆并检查泄漏 我有一个处理所有网络的类。它被称为ResponseOrganizer,其中有一个类方法: + (void)getSth:(void (^)(NSURLSessionDataTask *operation, NSArray *locales, id plainObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure { Connect
ResponseOrganizer
,其中有一个类方法:
+ (void)getSth:(void (^)(NSURLSessionDataTask *operation, NSArray *locales, id plainObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
Connection *connection = [Connection new];
connection.urlString = @"http://sample-file.bazadanni.com/download/txt/json/sample.json";
connection.requestMethodType = GET;
[connection fireWithSuccess:^(NSURLSessionDataTask *operation, NSArray *returnArray, id originalResponse) {
success(operation, returnArray, originalResponse);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation, error);
}];
}
其中,Connection
是单个“我的内部连接”对象:
以下是实施方案:
#import "Connection.h"
@interface Connection()
@property (weak,nonatomic) AFHTTPSessionManager *manager;
@end
@implementation Connection
#pragma mark - Connection groundwork
-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
self.manager = [AFHTTPSessionManager manager];
[self.manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,@[responseObject],nil);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
}];
}
@end
我在AFNetworking
中有一个category calling right方法。要简化它,请如下所示:
-(void)urlString:(NSString*)urlString withMethod:(RequestMethodType)method parameters:(NSDictionary*)parameters success:(void (^)(NSURLSessionDataTask *operation, id responseObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
switch (method) {
case GET: {
[self getWithURLString:urlString parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
}];
break;
}
}
[ResponseOrginizer getSth:^(NSURLSessionDataTask *operation, NSArray *locales, id plainObject) {
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
}];
例如,当我想在我的ViewController中发出请求时,我会这样做:
-(void)urlString:(NSString*)urlString withMethod:(RequestMethodType)method parameters:(NSDictionary*)parameters success:(void (^)(NSURLSessionDataTask *operation, id responseObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
switch (method) {
case GET: {
[self getWithURLString:urlString parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
}];
break;
}
}
[ResponseOrginizer getSth:^(NSURLSessionDataTask *operation, NSArray *locales, id plainObject) {
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
}];
当我在仪器中运行它时,我总是得到:
它在这里并不重要,它会落在成功/失败块上,它总是导致泄漏。我从中提取所有内容,并将其尽可能简单地放在github上。Github链接:
泄漏出现在此处:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
原因似乎与前面讨论的相同(或类似)-NSURLSession保留对委托的引用
按如下方式更改Connection.m中的代码以避免泄漏:
-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
AFHTTPSessionManager *manager = [Connection manager];
[manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,@[responseObject],nil);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
}];
}
+ (AFHTTPSessionManager*) manager
{
static dispatch_once_t onceToken;
static AFHTTPSessionManager *manager = nil;
dispatch_once(&onceToken, ^{
manager = [AFHTTPSessionManager manager];
});
return manager;
}
如果您需要处理多个会话,您可以使用另一种方法:完成会话时调用-[AFHTTPSessionManager invalidateSessionCancelingTasks:
,例如:
-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
success(operation,@[responseObject],nil);
[manager invalidateSessionCancelingTasks:YES];
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
failure(operation,error);
[manager invalidateSessionCancelingTasks:YES];
}];
}
(注意:如果要取消挂起的任务,请通过YES
,否则通过NO
。为什么选择Singleton?当我在AFHTTPSessionManager上同时启动两个连接时会发生什么情况?谢谢,[manager invalidateSessionCancelingTasks:YES]
works+(AFHTTPSessionManager*)manager{static dispatch_once\u t onceToken;static AFHTTPSessionManager*manager=nil;dispatch_once(&onceToken,^{manager=[AFHTTPSessionManager];});return manager;}有人能用swift提供此代码吗?