如何修复AFNetworking 3.1中的内存泄漏?

时间:2016-07-11 11:53:08

标签: ios objective-c afnetworking

我在仪器中测试应用程序,因此代码会导致内存泄漏。请告诉我如何解决它。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:configuration];

2 个答案:

答案 0 :(得分:1)

我不知道我是否迟到了,但回答那些碰到这个问题的人。这是预期的行为。完成会话后,您需要调用此invalidateSessionCancelingTasks:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

__weak typeof(self) welf = self;
[self.manager uploadTaskWithRequest:request fromFile:filePath progress:^(NSProgress * _Nonnull uploadProgress) {
    // progress block
} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    //Invalidate Session here
    [welf.manager invalidateSessionCancelingTasks:YES];
    // stuff needed to be done
}];

答案 1 :(得分:0)

您可以尝试以下代码:

static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        sessionConfiguration.HTTPMaximumConnectionsPerHost = 10;
         self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
});