首先,我是仪器和内存图的新手。试图检查我的应用程序是否存在内存泄漏,然后遇到了这个问题。它是具有MVVM体系结构的类的集合,该类具有以下功能:
我做了一些研究,以下是我的发现:
下面的内存图和仪器的介绍,代码和屏幕截图足够多,(代码类很大,因此我只附加最重要的部分):
视图控制器类
@objc final class HomeVC: UIViewController {
internal lazy var viewModel = { return HomeVM() }()
override func viewDidLoad() {
initVM()
}
private func initVM() {
viewModel.reloadPersonalization.addObserver(onChange: {[weak self] (reload) in
if reload {
self?.collectionView.reloadSections(IndexSet(integer: HomeSection.personalization.rawValue))
}
})
}
}
查看模型类
import SwiftyJSON
class HomeVM {
//MARK:- Observable
let reloadPersonalization = Observable<Bool>(value: false)
let isLoading = Observable<Bool>(value: false)
//MARK:- Properties
var personalizations = Personalization()
//MARK:- Initialization
init() {
}
//MARK:- Functions
public func fetchPersonalization() {
BaseApi.requestAPIService(apiService: MainMallService.showPersonalization, resultReceive: {
}, success: {[weak self] (json) in
self?.personalizations.updateProductList(json: json)
self?.reloadPersonalization.value = true
}, successWithError: {[weak self] (errorMsg) in
self?.reloadPersonalization.value = false
}, fail: {[weak self] (errorMsg) in
self?.reloadPersonalization.value = false
})
}
}
网络课程
import Moya
import SwiftyJSON
import Alamofire
class BaseApi {
//MARK: Properties
//MARK: Functions
open class func requestAPIService(apiService: TargetType, resultReceive: @escaping () -> Void ,success: @escaping (_ jsonResponse: JSON) -> (), successWithError: @escaping (_ error: String) -> (), fail: @escaping (_ errorMsg: String) -> ()) {
let provider = MoyaProvider<MultiTarget>(manager:NetworkManager.shared ,plugins: [NetworkLoggerPlugin(verbose: true)])
let target = MultiTarget(apiService)
provider.request(target) { result in
resultReceive()
switch result {
case let .success(moyaResponse):
let data = moyaResponse.data
let json = JSON(data)
let apiStatus = ApiStatus.initWithJson(json: json["status"])
switch apiStatus.status {
case .Success:
if json["response"].exists() {
let jsonResponse = json["response"]
success(jsonResponse)
}
case .Failed:
successWithError(StringConst.Error.internalError)
case .Unkown:
fail(StringConst.Error.unknownError)
}
case let .failure(error):
var errorMsg = ""
if error._code == NSURLErrorTimedOut {
errorMsg = StringConst.Connection.timeout
} else if error._code == NSURLErrorNotConnectedToInternet {
errorMsg = StringConst.Connection.noConnection
} else {
errorMsg = StringConst.Error.unknownError
}
fail(errorMsg)
}
}
}
}
此行代码上发生内存泄漏,内存图和仪器都再次指向该行;仅在调用成功块时才会发生泄漏,其他块(例如error和successWithError)不会触发内存泄漏。
let json = JSON(data)
感谢您的帮助!