今天用Instruments的Leaks工具 检测了一下app内存泄露
Leak checks 出现 一堆 ❌❌❌❌标记
点击Product-Prifile打开Leaks
屏幕快照 2019-01-10 下午3.49.29.png
1.点击红色按钮开始检测.
2.点击❌标记
3.选择Lasks-Leaks by backtrace 点击下面尖头
-
双击蓝色人头标记 直接打开内存泄露代码
A16F38C9-CB0E-4DEA-AE2A-6B2D0D7B3AD9.png
解决方法
把AFHTTPSessionManager做成一个单例就不会内存泄露啦
具体代码
static AFHTTPSessionManager *manager;
+(AFHTTPSessionManager *)sharedManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [AFHTTPSessionManager manager];
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;//移除null的字符串
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setTimeoutInterval:30];//30秒超时
[manager.requestSerializer setHTTPShouldHandleCookies:NO];//默认是yes
// 缓存策略
manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
// 4. 设置响应数据类型
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript", nil]];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
});
return manager;
}
然后在二次封装网络框架 获取 AFHTTPSessionManager的时候这样获取就行了,再次测试 就没有因为请求内存泄露了
AFHTTPSessionManager *manager = [SYBaseHttpConnection sharedManager];
网友评论