Nov 11, 2024
import BackgroundTasks
struct PingApp: App {
init() {
...
BGTaskScheduler.shared.register(forTaskWithIdentifier: BackgroundTasks.cleanDBIdentifier, using: nil) { task in
BackgroundTasks.shared.cleanDB()
BackgroundTasks.shared.scheduleBackgroundTask()
task.setTaskCompleted(success: true)
}
}
}
struct BackgroundTasks {
public func cleanDB() {
...
let oneWeekAgo = Calendar.current.date(byAdding: .day, value: -7, to: currentDate)!
try threadContext.delete(model: ServerResponse.self, where: #Predicate { $0.date < oneWeekAgo })
...
}
}