I have a loop which saves caller ids to the call directory extension. If there are too many records to save it will crash and XCode gives the error "Terminated due to memory issue". I've read elsewhere that the correct way to manage large loops is to use autoreleasepool
but it doesn't solve the problem in this case.
for i in stride(from: rows.count - 1, through: 0, by: -1) {
autoreleasepool {
//Separating the contact name from the phone number
let columns = rows[i].components(separatedBy: ",")
//Skip results which don't have both a name and number
if(columns.count == 2){
//Adding record to the call directory
context.addIdentificationEntry(withNextSequentialPhoneNumber: Int64(columns[0])!, label: columns[1])
}
}
}
If I remove the context.addIdentificationEntry()
line then it doesn't crash.
Is autoreleasepool
used correctly here or is there another way to prevent out-of-control memory usage?