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) {
}];
當我在儀器上運行它我總是越來越:
而且在這裏不要緊將土地成功/失敗塊上,它總是導致泄漏。我從中提取一切,並儘可能簡單地將它放在github上。 Github的鏈接: https://github.com/JakubMazur/SO41343532/
爲什麼Singleton?當我在AFHTTPSessionManager上同時觸發兩個連接時會發生什麼? – Kuba
@Kuba我已經更新了答案,請檢查。 – degapps
謝謝,'[manager invalidateSessionCancelingTasks:YES];'works – Kuba