Home About Notes

Ping v1.0.5

Nov 11, 2024

Available for immediate download is Ping version 1.0.5. 

This version fixes a bug I had upgrading from iOS 17 to 18 where SwiftData would no longer save anymore data. I don't actually know what cause this, but if you cleared history the widget would again start working. It got me thinking that it's probably worth clearing out the DB from time to time. Ping is not an app you open regularly, it mostly just in the widget and background so I couldn't just write some code on open to clear out the DB id have to use BackgroundTasks to schedule and run tasks.

Basics of how it works:
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 })
        ...
    }
}