I have a swift program that reads a series of CSV files into core data. Once that is complete I save the view content to persistent store with moc.save. After this call I would like to be able to run a checkpoint from within the swift program to flush the records in the .sqlite-wal file to the .sqlite file. I have tried with no success. Below is the my class that handles the file reading and saving. The code I have attempted to use to run the checkpoint is listed below that.
class UpdateCoreDataTest: ObservableObject {
@Published var isLoading: Bool = true
var moc: NSManagedObjectContext
init(moc: NSManagedObjectContext) {
self.moc = moc
Task {
await IsDataStoreEmpty(moc: self.moc)
let fund1 = CSVtoCoreData(fundName: "Fund1", fileName: "Fund1.csv")
let fund2 = CSVtoCoreData(fundName: "Fund2", fileName: "Fund2.csv")
let fund3 = CSVtoCoreData(fundName: "Fund3", fileName: "Fund3.csv")
await fund1.CSVtoCoreDataG(moc: self.moc)
await fund2.CSVtoCoreDataG(moc: self.moc)
await fund3.CSVtoCoreDataG(moc: self.moc)
do {
try moc.save()
} catch {
print("Error saving. \(error)")
}
DispatchQueue.main.async {
self.isLoading = false
}
}
} // end init
}
let fileURL = URL(fileURLWithPath: "/Users/Chris/Downloads/Persistent Store 2/Funds.sqlite")
var dataBase: OpaquePointer?
if sqlite3_open(fileURL.path, &dataBase) != SQLITE_OK {
print("Unable to open sqlite file")
} else {
print("Succesfully opened sqlite file")
}
sqlite3_wal_checkpoint(dataBase, "Index Funds.sqlite" )
sqlite3_close(dataBase)