不可用的AFNetworking内存泄漏

时间:2016-12-27 10:38:17

标签: ios objective-c memory-leaks 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);
    }];
}

其中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内有一个类别调用权限方法。为了简化它,看起来像这样:

-(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;
        }
}

当我想在我的ViewController中提出请求时,我就这样做:

[ResponseOrginizer getSth:^(NSURLSessionDataTask *operation, NSArray *locales, id plainObject) {

} failure:^(NSURLSessionDataTask *operation, NSError *error) {

}];

当我在乐器中运行时,我总是得到:

enter image description here

这里无关紧要,它会落在成功/失败区块上,它总会导致泄漏。我从中提取所有内容并尽可能简单地将它放在github上。 Github链接: https://github.com/JakubMazur/SO41343532/

1 个答案:

答案 0 :(得分:7)

泄漏出现在这里:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

似乎原因与讨论的here相同(或类似) - 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