Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/691a5ca1-2b1c-8010-8cbd-8797f95aa8d9
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Your idea of a small aggregated engine is good — the trick is to design it so it’s pure and reusable, and you just plug it into whatever background runtime you can get. ==== Conceptually: <syntaxhighlight lang="swift">actor DrivingEngine { struct Event { let date: Date let isDriving: Bool let speed: CLLocationSpeed? } private var currentSession: DrivingSession? private var accumulatedSeconds: TimeInterval = 0 func handle(event: Event) -> DrivingUpdate { // Update state machine based on transitions // e.g. stationary -> driving, driving -> stopped, etc. // Use timestamps to accumulate driving time } func resume(from storedSession: DrivingSession?) { // Rehydrate from SwiftData / persisted state } } </syntaxhighlight> Then you have different “front-ends” that feed events into it: * Foreground: CMMotion + GPS didUpdateLocations. * Background (while location is alive): same, but no UI. * BGTask / app relaunch: synthetic events built from Core Motion history (queryActivityStarting) and maybe historical location, if you’re storing that. Your SwiftUI view just observes some published view model that’s driven by this actor; in the background scenario you skip the UI part and just call the engine then schedule notifications. Important: this engine doesn’t rely on timers; it operates only off timestamps from events, which works fine whether events are real-time or “I fetched a batch an hour later”.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)