使用 AFHTTPSessionManager 时内存泄漏
Memory leak when use AFHTTPSessionManager
环境:
- iOS13/iOS10
- Xcode11.1
- AFNetworking 版本:3.2.1
- Swift 4.2
说明:
当使用 AFNetworking post 或获取 Json 数据时,会导致内存泄漏,我创建了一个 AFHTTPSessionManager 是这样的:
private static func ConfigureAFManager(requestSerialization: HttpSerializationType = HttpSerializationType.HTTP,
responseSerialization: HttpSerializationType = HttpSerializationType.JSON,
timeout: TimeInterval = 30,
headers: [String: String]? = nil
) -> AFHTTPSessionManager {
let AFManager = AFHTTPSessionManager()
if requestSerialization == .JSON {
AFManager.requestSerializer = AFHTTPRequestSerializer()
}
if responseSerialization == .HTTP {
AFManager.responseSerializer = AFHTTPResponseSerializer()
}
for (key, value) in headers ?? [:] {
AFManager.requestSerializer.setValue(value, forHTTPHeaderField: key)
}
AFManager.requestSerializer.timeoutInterval = timeout;
return AFManager
}
然后我用它来执行post动作:
static func POST(httpURL: String,
parameter: Any?,
timeout: TimeInterval = 30,
headers: [String: String]? = nil,
requestSerialization: HttpSerializationType = HttpSerializationType.HTTP,
responseSerialization: HttpSerializationType = HttpSerializationType.JSON,
success: ((Any?) -> Void)?,
fail: ((Error?) -> Void)?) -> Void
{
let AFManager = self.ConfigureAFManager(requestSerialization: requestSerialization, responseSerialization: responseSerialization, timeout: timeout, headers: headers)
AFManager.post(httpURL, parameters: parameter, progress: nil, success: { (task, response) in
success?(response)
}) { (task, error) in
fail?(error)
}
}
当我点击Xcode上的Debug Memory Graph
时,我发现,AFHTTPSessionManager
和__NSURLSessionLocal
之间有一些循环引用。
Cycle reference
这是否仅在 AFNetworking 的 3.2.1 版本中发生?
我知道为什么,因为 AFNetWorking 使用 URLSession 它的委托将建立一个强引用,所以在使用 AFHttpSessionManager 之后应该调用 invalidateSessionCancelingTasks
环境:
- iOS13/iOS10
- Xcode11.1
- AFNetworking 版本:3.2.1
- Swift 4.2
说明: 当使用 AFNetworking post 或获取 Json 数据时,会导致内存泄漏,我创建了一个 AFHTTPSessionManager 是这样的:
private static func ConfigureAFManager(requestSerialization: HttpSerializationType = HttpSerializationType.HTTP,
responseSerialization: HttpSerializationType = HttpSerializationType.JSON,
timeout: TimeInterval = 30,
headers: [String: String]? = nil
) -> AFHTTPSessionManager {
let AFManager = AFHTTPSessionManager()
if requestSerialization == .JSON {
AFManager.requestSerializer = AFHTTPRequestSerializer()
}
if responseSerialization == .HTTP {
AFManager.responseSerializer = AFHTTPResponseSerializer()
}
for (key, value) in headers ?? [:] {
AFManager.requestSerializer.setValue(value, forHTTPHeaderField: key)
}
AFManager.requestSerializer.timeoutInterval = timeout;
return AFManager
}
然后我用它来执行post动作:
static func POST(httpURL: String,
parameter: Any?,
timeout: TimeInterval = 30,
headers: [String: String]? = nil,
requestSerialization: HttpSerializationType = HttpSerializationType.HTTP,
responseSerialization: HttpSerializationType = HttpSerializationType.JSON,
success: ((Any?) -> Void)?,
fail: ((Error?) -> Void)?) -> Void
{
let AFManager = self.ConfigureAFManager(requestSerialization: requestSerialization, responseSerialization: responseSerialization, timeout: timeout, headers: headers)
AFManager.post(httpURL, parameters: parameter, progress: nil, success: { (task, response) in
success?(response)
}) { (task, error) in
fail?(error)
}
}
当我点击Xcode上的Debug Memory Graph
时,我发现,AFHTTPSessionManager
和__NSURLSessionLocal
之间有一些循环引用。
Cycle reference 这是否仅在 AFNetworking 的 3.2.1 版本中发生?
我知道为什么,因为 AFNetWorking 使用 URLSession 它的委托将建立一个强引用,所以在使用 AFHttpSessionManager 之后应该调用 invalidateSessionCancelingTasks