gpt4 book ai didi

ios - 无法追踪的 AFNetworking 内存泄漏

转载 作者:行者123 更新时间:2023-12-01 17:24:37 25 4
gpt4 key购买 nike

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

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

最佳答案

泄漏出现在这里:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

看来原因与讨论的相同(或相似)here - NSURLSession 保留对委托(delegate)的引用。

像这样更改 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;
}

如果您需要处理多个 session ,您可以使用另一种方法:在完成 session 后调用 -[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)。

关于ios - 无法追踪的 AFNetworking 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41343532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com