ForEach 中的 SwiftUI 内存泄漏问题

Har*_*hat 8 memory-leaks ios swiftui swiftui-list

我在屏幕中有一个垂直列表来显示图像类别,每个类别/列表都包含水平显示的图像列表。(附图供参考)

现在,当我水平或垂直滚动​​时,应用程序由于内存泄漏而崩溃。我想很多人都在ForEach循环中面临这个问题。

我也尝试过使用List代替ForEachScrollView来垂直/水平滚动,但不幸的是面临同样的问题。

下面的代码是创建垂直列表的主视图:

@ObservedObject var mainCatData = DataFetcher.sharedInstance

var body: some View {
    
    NavigationView {
        VStack {
            ScrollView(showsIndicators: false) {
                LazyVStack(spacing: 20) {
                    ForEach(0..<self.mainCatData.arrCatData.count, id: \.self) { index in
                        self.horizontalImgListView(index: index)
                    }
                }
            }
        }.padding(.top, 5)
        .navigationBarTitle("Navigation Title", displayMode: .inline)
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码在每个类别内创建水平列表,我使用了LazyHStackForEach循环和ScrollView

@ViewBuilder
func horizontalImgListView(index : Int) -> some View {
    
    let dataContent = self.mainCatData.arrCatData[index]

    VStack {
     
        HStack {
            Spacer().frame(width : 20)
            Text("Category \(index + 1)").systemFontWithStyle(style: .headline, design: .rounded, weight: .bold)
            Spacer()
        }
        
        ScrollView(.horizontal, showsIndicators: false) {
            LazyHStack(spacing: 20) {
                ForEach(0..<dataContent.catData.count, id: \.self) { count in
                                                                                    
                    VStack(spacing : 0) {
                        VStack(spacing : 0) {
                            
                            if let arrImgNames = themeContent.catData[count].previewImgName {

                                // Use dynamic image name and it occurs app crash & memory issue and it reached above 1.0 gb memory
                                Image(arrImgNames.first!).resizable().aspectRatio(contentMode: .fit)
                                                                                                                                                                
                               // If I will use the same image name then there is no memory issue and it consumes only 75 mb
                               // Image("Category_Image_1").resizable().aspectRatio(contentMode: .fit)                              
                            }
                        }.frame(width: 150, height: 325).cornerRadius(8.0)
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是我用来从 json 文件获取图像并将其显示在列表中的数据模型

class DataFetcher: ObservableObject {
    
    static let sharedInstance = DataFetcher()
    @Published var arrCatData = [CategoryModel]()
     
    init() {
                
        do {
            if let bundlePath = Bundle.main.url(forResource: FileName.CategoryData, withExtension: "json"),
               
               let jsonData = try? Data(contentsOf: bundlePath) {
                
                let decodedData = try JSONDecoder().decode([CategoryModel].self, from: jsonData)
                DispatchQueue.main.async { [weak self] in
                    self?.arrCatData = decodedData
                }
            }
        } catch {
            print("Could not load \(FileName.CategoryData).json data : \(error)")
        }
    }
}

struct CategoryModel : Codable , Identifiable {
    let id: Int
    let catName: String
    var catData: [CategoryContentDataModel]
}

struct CategoryContentDataModel : Codable {
    var catId : Int
    var previewImgName : [String]
}
Run Code Online (Sandbox Code Playgroud)

崩溃日志:

malloc: can't allocate region
:*** mach_vm_map(size=311296, flags: 100) failed (error code=3)
(82620,0x106177880) malloc: *** set a breakpoint in malloc_error_break to debug
2021-07-01 18:33:06.934519+0530 [82620:5793991] [framework] CoreUI: vImageDeepmap2Decode() returned 0.
2021-07-01 18:33:06.934781+0530 [82620:5793991] [framework] CoreUI: CUIUncompressDeepmap2ImageData() fails [version 1].
2021-07-01 18:33:06.934814+0530 [82620:5793991] [framework] CoreUI: Unable to decompress 2.0 stream for CSI image block data. 'deepmap2'
(82620,0x106177880) malloc: can't allocate region
:*** mach_vm_map(size=311296, flags: 100) failed (error code=3)
(82620,0x106177880) malloc: *** set a breakpoint in malloc_error_break to debug
Run Code Online (Sandbox Code Playgroud)

注意:类别的所有图像仅从资产加载,如果我在循环中使用图像的静态名称,则不存在内存压力,并且仅消耗 75 mb。

我认为存在图像缓存问题。即使我从资产加载图像,我是否也必须管理图像缓存?

谁能帮我解决这个问题?任何帮助都感激不尽。谢谢!!

在此输入图像描述

小智 0

尝试不要在 ForEach 中使用显式 self。我的 SwiftUI 视图中出现了一些奇怪的泄漏,切换到隐式 self 似乎可以消除它们。