suhas
suhas

Reputation: 301

How to fix memory leak in AFNetworking 3.1?

I'm testing app in the instruments so below code getting memory leak. Please tell me how to fix it.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

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

Upvotes: 0

Views: 1219

Answers (2)

Mirza Bilal
Mirza Bilal

Reputation: 1050

I don't know if i am late to the party, but answering for those who bumped into this. This is the expected behaviour. when you are done with the session you need to call this 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
}];

Upvotes: 1

Ekta Padaliya
Ekta Padaliya

Reputation: 5799

You can try this code:

static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

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

Upvotes: 0

Related Questions