No not making any save. I mean I intend to. I was placing all items in the loop into a holding array. Then outside the loop saving that array into swiftData. The save is instant when I do. However the loop creating the model objects is what takes all the time.
This loop through 30k+ lines of a CSV file takes 90s
@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]
)
}
This one takes 3 seconds. its only adding the @Model macro that makes it unusably slow
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]
)
}