首页 > 解决方案 > SwiftUI - 内存管理

问题描述

好的,所以我希望改善我的应用程序中的内存。

我已为我的项目启用Live Memory Allocation,我正在使用Debug Graph Tool. 我正在查看回溯,并遇到问题,老实说,这对我来说没有意义。据我所知,我已经删除了强引用,但我的部分代码遇到了问题,我只是不理解/看到问题。这方面的例子是:

struct ProductScrollView: UIViewRepresentable {
    private let view = UIScrollView()
    func makeCoordinator() -> Coordinator {
        return ProductScrollView.Coordinator(parent1: self)
    }
    @State var currentPage: Int
    @Binding var products: [ProductModel]
    func makeUIView(context: Context) ->  UIScrollView {

        if view.superview == .none {
        let childView = UIHostingController(rootView: ProductCell(products: $products, currentPage: currentPage, pageCount: products.count)
        ) <---- Debugger seems to indicate this with the message "Thread 1"



        childView.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * CGFloat((products.count)))
        view.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * CGFloat((products.count)))
        
        view.addSubview(childView.view)
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.contentInsetAdjustmentBehavior = .never
        view.isPagingEnabled = true
        view.delegate = context.coordinator
        }
        return view
    }
    func updateUIView(_ uiView: UIScrollView, context: Context) {
        uiView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * CGFloat((products.count)))
        for i in 0..<uiView.subviews.count {
            uiView.subviews[i].frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * CGFloat((products.count)))
        }
    }
    
    class Coordinator: NSObject, UIScrollViewDelegate {
        var parent: ProductScrollView

        init(parent1: ProductScrollView){
            parent = parent1
        }
        
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            let index = Int(scrollView.contentOffset.y / UIScreen.main.bounds.height)
            parent.currentPage = index
      
        }

        deinit {
            print("Coordiante scroll view desroted")
        }
    }
}

这对我来说并不完全有意义,但是,我相信这条线:

 return ProductScrollView.Coordinator(parent1: self)

可能是问题吗?

我什至会让调试器指向在我的onAppear方法上执行的函数。

有人可以帮助我如何更好地理解这些问题并消除它们吗?

编辑 -

为了进一步研究,这条线出现在我的回溯中:

static var FEEDBACK = FeedbackAPI()

是因为每次都创建一个新实例吗?

标签: memory-managementmemory-leaksswiftui

解决方案


按照设计,我们需要在内部实例化视图makeView,因此 SwiftUI 将 UIView 提升周期管理为与可表示视图相同,例如

struct ProductScrollView: UIViewRepresentable {

    func makeCoordinator() -> Coordinator {
        return ProductScrollView.Coordinator(parent1: self)
    }

    @State var currentPage: Int
    @Binding var products: [ProductModel]

    func makeUIView(context: Context) ->  UIScrollView {
        let view = UIScrollView()   // << here !!

// ... other code
}

推荐阅读