More Adventures in Vibe Coding…



This content originally appeared on DEV Community and was authored by Leif

It looks like the LLM totally forgot to actually do anything with the Apple Watch background scheduler and I didn’t catch this on the first iteration. And this is why we need to check, check, and check again every line we “vibe code”:

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate {
    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
        for task in backgroundTasks {
            switch task {
            case let backgroundRefreshTask as WKApplicationRefreshBackgroundTask:
                Task {
                    print("Performing background refresh for TempStick data")
                }
                backgroundRefreshTask.setTaskCompletedWithSnapshot(false)

            case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
                urlSessionTask.setTaskCompletedWithSnapshot(false)

            default:
                task.setTaskCompletedWithSnapshot(false)
            }
        }
    }
}

chmod a+x ./facepalm.sh; ./facepalm.sh

**Note: there is no actual shell script in the app called facepalm.sh

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate {
  func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    for task in backgroundTasks {
      switch task {
      case let backgroundRefreshTask as WKApplicationRefreshBackgroundTask:
        Task {
          defer { backgroundRefreshTask.setTaskCompletedWithSnapshot(false) }
          print("Performing background refresh for TempStick data")

          await WatchModel.shared.fetchAllReadings()

          WatchModel.shared.scheduleBackgroundRefresh()
        }

      case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
        urlSessionTask.setTaskCompletedWithSnapshot(false)

      default:
        task.setTaskCompletedWithSnapshot(false)
      }
    }
  }
}

That’s better! Now we should actually be scheduling background tasks in the Apple Watch, which is important…because for battery purposes and so on, Apple doesn’t let you arbitrarily run long running processes in the watch (like continuously polling our temperature sensors) in the background; at most you can “request” a background task lasting no more than a few seconds is executed at a given interval and the watch will decide whether or not to let it happen. Currently configured to try at the max given interval specified in the user settings per sensor.

Code fixes pushed to public repo https://github.com/leifdroms/TempStickMonitor-public. Thank you for listen to this public service message.


This content originally appeared on DEV Community and was authored by Leif