memory-management - 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()
是因为每次都创建一个新实例吗?
解决方案
按照设计,我们需要在内部实例化视图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
}
推荐阅读
- javascript - 使用 ajax 将数组值从 PHP 传递到 JavaScript
- python - 如何查询父类上的两个子类的数据?(sqlalchemy)
- c++ - 类接收一个 char const * 作为参数(如何在代码中声明 txt.)
- javascript - 你能用anime.js为文本内容制作动画吗?
- rust - 我应该遵循哪个函数生命周期'a 或只是作用域生命周期?
- ios - 有没有办法在 SwiftUI 中仅针对某些视图强制使用横向模式?
- java - 嵌套在 LockoutRealm 中时如何在 Java 中获取 Tomcat CredentialHandler
- asp.net - 为什么 Visual Studio 不启动我的网站?
- javascript - 循环中的 Onclick 函数使用循环的最后一次迭代而不是当前迭代
- javascript - 如何将引导列中图像的位置垂直更改几个像素