SwiftData模型实例创建速度极慢,如何优化大规模数据处理?
SwiftData大规模数据集实例创建性能问题及官方反馈
问题背景
在处理包含3万+行、总计约23万个元素的CSV文件时,使用SwiftData创建模型实例并存储的总耗时约90秒,而此前使用CoreData完成相同操作仅需3秒。经排查,性能瓶颈完全源于SwiftData模型实例的创建过程。
苹果官方回复
提交代码级支持(DTS)工单并提供测试项目与数据后,苹果官方反馈:
针对反馈编号#FB13432637,DTS无法提供解决方案,该问题仍在调查中,请通过bug报告持续跟踪。
验证测试
为确认问题根源,执行了以下测试步骤:
- 移除所有SwiftData保存操作,仅保留模型对象创建逻辑
- 创建去掉
@Model宏的SwiftData模型类副本 - 将
isAutosaveEnabled设为false
测试结果显示,使用无@Model宏的副本类执行循环仅需3秒,由此确定是@Model宏带来的实例创建开销导致了30倍的耗时增加。
代码对比
带@Model的SwiftData代码(耗时90+秒)
@Model class TimetableData { var arrivalTime : Int = 0 var departureTime: Int = 0 var departureRoute : Int = 0 var directionOfTravel : Int = 0 var dutyNumber : Int = 0 var facilityIdentifier : String = "" var locationType : String = "" var mode : String = "" var nonStopStatus : Int = 0 var recordIdentifier : String = "" var timetableIdentifier : Int = 0 var trainNumber : String = "" var tripNumber : Int = 0 var tripStartSite : String = "" var uniqueLocationCode : String = "" init(arrivalTime: Int = 0, departureTime: Int = 0, departureRoute: Int = 0, directionOfTravel: Int = 0, dutyNumber: Int = 0, facilityIdentifier: String = "", locationType: String = "", mode: String = "", nonStopStatus: Int = 0, recordIdentifier: String = "", timetableIdentifier: Int = 0, trainNumber: String = "", tripNumber: Int = 0, tripStartSite: String = "", uniqueLocationCode: String = "") { // 注:原代码中`self.name = name`存在未定义变量问题,疑似笔误 self.arrivalTime = arrivalTime self.departureTime = departureTime self.departureRoute = departureRoute self.directionOfTravel = directionOfTravel self.dutyNumber = dutyNumber self.facilityIdentifier = facilityIdentifier self.locationType = locationType self.mode = mode self.nonStopStatus = nonStopStatus self.recordIdentifier = recordIdentifier self.timetableIdentifier = timetableIdentifier self.trainNumber = trainNumber self.tripNumber = tripNumber self.tripStartSite = tripStartSite self.uniqueLocationCode = uniqueLocationCode } } for line in CSVFile { var columnValue = line.components(separatedBy: ",") let timeTableDataEntry = TimetableData( arrivalTime: Int(columnValue[0])!, departureTime: Int(columnValue[1])!, departureRoute: Int(columnValue[2])!, directionOfTravel: Int(columnValue[3])!, dutyNumber: Int(columnValue[4])!, facilityIdentifier: columnValue[5], locationType: columnValue[6], mode: columnValue[7], nonStopStatus: Int(columnValue[8])!, recordIdentifier: columnValue[9], timetableIdentifier: Int(columnValue[10])!, trainNumber: columnValue[11], tripNumber: Int(columnValue[12])!, tripStartSite: columnValue[13], uniqueLocationCode: columnValue[14] ) }
无@Model的测试代码(耗时约3秒)
class TimetableDataForTesting { var arrivalTime : Int = 0 var departureTime: Int = 0 var departureRoute : Int = 0 var directionOfTravel : Int = 0 var dutyNumber : Int = 0 var facilityIdentifier : String = "" var locationType : String = "" var mode : String = "" var nonStopStatus : Int = 0 var recordIdentifier : String = "" var timetableIdentifier : Int = 0 var trainNumber : String = "" var tripNumber : Int = 0 var tripStartSite : String = "" var uniqueLocationCode : String = "" init(arrivalTime: Int = 0, departureTime: Int = 0, departureRoute: Int = 0, directionOfTravel: Int = 0, dutyNumber: Int = 0, facilityIdentifier: String = "", locationType: String = "", mode: String = "", nonStopStatus: Int = 0, recordIdentifier: String = "", timetableIdentifier: Int = 0, trainNumber: String = "", tripNumber: Int = 0, tripStartSite: String = "", uniqueLocationCode: String = "") { // 注:原代码中`self.name = name`存在未定义变量问题,疑似笔误 self.arrivalTime = arrivalTime self.departureTime = departureTime self.departureRoute = departureRoute self.directionOfTravel = directionOfTravel self.dutyNumber = dutyNumber self.facilityIdentifier = facilityIdentifier self.locationType = locationType self.mode = mode self.nonStopStatus = nonStopStatus self.recordIdentifier = recordIdentifier self.timetableIdentifier = timetableIdentifier self.trainNumber = trainNumber self.tripNumber = tripNumber self.tripStartSite = tripStartSite self.uniqueLocationCode = uniqueLocationCode } } for line in CSVFile { var columnValue = line.components(separatedBy: ",") let timeTableDataEntry = TimetableDataForTesting( arrivalTime: Int(columnValue[0])!, departureTime: Int(columnValue[1])!, departureRoute: Int(columnValue[2])!, directionOfTravel: Int(columnValue[3])!, dutyNumber: Int(columnValue[4])!, facilityIdentifier: columnValue[5], locationType: columnValue[6], mode: columnValue[7], nonStopStatus: Int(columnValue[8])!, recordIdentifier: columnValue[9], timetableIdentifier: Int(columnValue[10])!, trainNumber: columnValue[11], tripNumber: Int(columnValue[12])!, tripStartSite: columnValue[13], uniqueLocationCode: columnValue[14] ) }
内容的提问来源于stack exchange,提问作者RyanTCB
相关产品推荐
相关产品推荐

