【问题标题】:Speed Up a list加快列表速度
【发布时间】:2019-12-28 14:00:13
【问题描述】:

我正在尝试加快我的应用程序的速度,不幸的是,目前它在使用 Swift Ui 执行某些搜索和数据列表时非常缓慢。

首先我有一个描述我的对象机场的数据模型,称为 AirportModel

import Foundation

class AirportModel : Identifiable , Codable{
    var aptICAO : String
    var aptName : String
    var aptCity : String
    var aptCountry :String
    var aptIATA : String

init(aptICAO: String, aptName: String, aptCity: String, aptCountry: String, aptIATA: String) {
    self.aptICAO = aptICAO
    self.aptName = aptName
    self.aptCity = aptCity
    self.aptCountry = aptCountry
    self.aptIATA = aptIATA
}

}

我有一个本地文件 apt.json,其中包含我机场的所有数据信息(我从网上下载了这个 json,里面大约有 29000 个机场)

所以当我第一次运行应用程序时,使用以下函数,我创建并保存了 AirportModel 类型的 airportVector。

func openfilejson (fileName : String) {
        if let path = Bundle.main.path(forResource: fileName, ofType: "json") {
            do {
                let fileUrl = URL(fileURLWithPath: path)
                let datafile = try Data(contentsOf: fileUrl,options: .mappedIfSafe)
                let json = JSON(data:datafile)
                objectWillChange.send()
                for (key,_) in json {

                    let airport = AirportModel(aptICAO: "", aptName: "", aptCity: "", aptCountry: "", aptIATA: "")
                    airport.aptName = json[key]["name"].stringValue
                    airport.aptCity = json[key]["city"].stringValue
                    airport.aptCountry = json[key]["country"].stringValue
                    airport.aptIATA = json[key]["iata"].stringValue
                    airport.aptICAO = json[key].stringValue
                    airportVector.append(airport)

                }

                debugPrint("nel vettore airport Vector ci sono \(airportVector.count) aeroporti")
                save2() // to save airport vector in the plistfile
                debugPrint("SALVATO IN MEMORIA ")

            } catch {
                 print("ERRORE OPEN FILE AEROPORTI")
            }
        }
    }

所有,这个工作正常,一旦应用程序第一次是 ru,就会创建内部有 29000 个机场的向量。

现在是大问题。

在一个视图中,我尝试使用 SwiftUI 和 searchBar 来列出这个机场。

问题是,由于要打开的数据量很大,当我用列表加载视图时;数据需要很长时间才能列出,而且当我吃午饭时,搜索一切都卡住了!

知道如何处理或加速如此大量的数据,以避免应用卡在加载以下视图!


import SwiftUI

struct ContentView: View {
    @ObservedObject var dm: DataManager
    @State private var searchTerm : String = ""
    var body: some View {
        VStack {
            Text("List")
            SearchBar(text: $searchTerm).shadow(radius: 10)
            List(dm.airportVector.filter{
                 $0.aptCity.localizedCaseInsensitiveContains(searchTerm)
                    || $0.aptName.localizedCaseInsensitiveContains(searchTerm)
            }){ item in
                HStack {

                    Text(item.aptICAO).bold().font(Font.system(size:20)).frame(width: 90, height: 10)
                    //
                    Text(item.aptName).font(Font.system(size: 15))
                    Spacer()
                    VStack(alignment: .leading) {

                        Text(item.aptCity).font(Font.system(size: 10)).font(.subheadline)
                        Spacer()
                        Text(item.aptCountry).font(Font.system(size: 10))

                    }
                }
            }
        }
    }
}

提前致谢。 达米亚诺

【问题讨论】:

    标签: arrays json filter model swiftui


    【解决方案1】:

    使您的数据符合Identifiable 并在您的List 内部使用ForEach 和一个单独的视图来呈现项目:

    List {
      ForEach(items) {
        MyItem(item)
      }
    }
    

    通过使 MyItem 符合 Equatable 并定义 == 函数,仅在项目发生更改时才渲染它们。通过添加断点或print 语句确保正在执行== 函数。如果它没有被执行,请参阅这篇文章以获得解决方法:

    https://swiftui-lab.com/equatableview/


    如果上述建议有效,但您发现自己在初始渲染后对列表进行了大规模修改,请尝试在这些修改期间使用 id()。见https://swiftui-lab.com/swiftui-id/


    如果上述建议不足以改善问题,您可能需要更自定义的解决方案。使用 TableView 或 CollectionView,见https://github.com/apptekstudios/ASCollectionView

    【讨论】:

    • 肯定在列表中使用 ForEach 会加快很多速度,现在我正在尝试加快搜索速度。我尝试在搜索 Xcode 上使用 id: \.self 说我的模型需要符合 hasable。任何提示如何创建,
    • 我添加了 Equatable 和 Hasable,但不知道如何使用...stackoverflow.com/questions/59580088/swiftui-big-list-search
    • 第 1 步:接受 + 点赞这个答案?。第 2 步:使用示例代码打开一个新问题。但是,使问题中的示例代码成为重现问题所需的最低限度。不要只是将整个应用程序的代码转储在那里,因为这会使我们更难为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多