2016-12-27 29 views
0

TL; DR:克隆和檢查泄漏自己https://github.com/JakubMazur/SO41343532/無法聯絡AFNetworking內存泄露

我有一個類,處理我的所有網絡。這就是所謂的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/

回答

4

泄漏出現在此處:

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)。

+0

爲什麼Singleton?當我在AFHTTPSessionManager上同時觸發兩個連接時會發生什麼? – Kuba

+0

@Kuba我已經更新了答案,請檢查。 – degapps

+0

謝謝,'[manager invalidateSessionCancelingTasks:YES];'works – Kuba