Jak*_*kub 1 memory-leaks objective-c ios afnetworking nsurlsession
TL; DR:自己克隆并检查泄漏https://github.com/JakubMazur/SO41343532/
我有一个处理我所有网络的课程.它被调用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);
}];
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我有一个类别调用正确的方法AFNetworking.为了简化它,看起来像这样:
-(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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我想在我的ViewController中提出请求时,我就这样做:
[ResponseOrginizer getSth:^(NSURLSessionDataTask *operation, NSArray *locales, id plainObject) {
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
}];
Run Code Online (Sandbox Code Playgroud)
当我在乐器中运行时,我总是得到:
它在这里无关紧要,它会落在成功/失败区块上,它总会导致泄漏.我从中提取所有内容并尽可能简单地将它放在github上.Github链接:https: //github.com/JakubMazur/SO41343532/
泄漏出现在这里:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
Run Code Online (Sandbox Code Playgroud)
似乎原因与此处讨论的相同(或类似)- 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;
}
Run Code Online (Sandbox Code Playgroud)
如果您需要处理多个会话,您可以使用另一种方法:-[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];
}];
}
Run Code Online (Sandbox Code Playgroud)
(注意:YES如果要取消挂起的任务,NO则通过,否则).
| 归档时间: |
|
| 查看次数: |
1959 次 |
| 最近记录: |