Watch: give GlucoseStore its own Core Data stack - #2475
Open
loopkitdev wants to merge 1 commit into
Open
Conversation
On the watch, CarbStore and GlucoseStore shared a single PersistenceController (one NSManagedObjectContext). GlucoseStore drives its context exclusively with the async context.perform API; CarbStore uses performAndWait. Mixing those two concurrency models on one serial context races, and can produce a torn read of a freshly-inserted CachedGlucoseObject where a required attribute (startDate) reads nil — crashing in Date._unconditionallyBridgeFromObjectiveC at GlucoseStore.addGlucoseSamples. Give GlucoseStore its own Core Data stack so it never shares a context with CarbStore. controllerInLocalDirectory gains a defaulted name parameter (existing callers unchanged); the watch's glucose store uses a dedicated directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On the watch,
CarbStoreandGlucoseStoreare constructed with the samePersistenceController(one sharedNSManagedObjectContext). But the two stores drive that context with different concurrency models:GlucoseStore→ asynccontext.perform { }CarbStore→queue.async { context.performAndWait { } }Mixing the Swift-async
performwith GCD-dispatchedperformAndWaiton a single serial context lets the two overlap, which can produce a torn read of a freshly-insertedCachedGlucoseObject— a required attribute (startDate) readsnil, and the app crashes bridging it:This was confirmed by two crash reports symbolicated end-to-end (system frames via matching watchOS DeviceSupport, LoopKit frames via the exact build dSYM). It matches the observed behavior: intermittent, and it disappears under the Xcode debugger (the debugger reschedules threads and closes the race window). It also explains the paradox that
create()setsstartDateandsave()(which validates the required attribute) succeeds two lines before the nil read — only a concurrent mutation of the shared context can do that within one block.Fix
Give
GlucoseStoreits own Core Data stack on the watch so it never shares a context withCarbStore.LoopCore/PersistenceController.swift:controllerInLocalDirectory(named:)gains a defaultednameparameter ("com.loopkit.LoopKit"), so every existing caller is unchanged.WatchApp Extension/Managers/LoopDataManager.swift: the watch glucose store now usescontrollerInLocalDirectory(named: "com.loopkit.LoopKit.Glucose").After this, the shared context has only
CarbStore(allperformAndWait), and glucose has a dedicated context driven only by asyncperform(the HealthKitperformAndWaitpaths are dormant on the watch — nohkSampleStore). Neither context mixes concurrency models, so the cross-store race is gone.Caveat
Existing watch users' cached glucose lives in the shared
com.loopkit.LoopKitstore; after this change the glucose store reads from a newcom.loopkit.LoopKit.Glucosedirectory and starts empty, re-backfilling from the phone on next launch. This is harmless — the watch glucose cache is ephemeral (4h) and re-populated via the existing backfill request.Notes / follow-ups (not in this PR)
NSManagedObjectContext.purgeObjectscombines a batch delete withmergeChanges(fromRemoteContextSave:)andrefreshAllObjects()on the same context; andGlucoseStore.saveSamplesToHealthKitreads managed objects (quantitySample) outside theirperformblock (iOS-only path).Test plan
startDatecrash.