CareKit icon indicating copy to clipboard operation
CareKit copied to clipboard

fix: KeyPath crash when querying CareStore

Open cbaker6 opened this issue 1 year ago • 0 comments

The framework crashes when you stream using: OCKOutcomeQuery, OCKCarePlanQuery, OCKContactQuery, OCKPatientQuery, OCKTaskQuery. Most likely happens with a standard fetch as well, but I didn't test those...

Resulting in:

image image

This can be replicated by using a simple streaming query within a view:

// A view that has the CareStore in it's environment 
struct SimpleCareView: View {

    @CareStoreFetchRequest(query: query()) private var outcomes

    var body: some View {
         // I want to display outcomes...
    }

    // Simple query
    static func query() -> OCKOutcomeQuery {
        OCKOutcomeQuery(for: Date())
    }
}

This most likely results from NSSortDescriptor not liking keyPaths of value types and prefers reference types (I'm guessing as I'm seen some other issues in the past with value type keyPaths and using NS...). In addition, since we are querying from CoreData, we should probably use the CD representations. Lastly, all other uses of NSSortDescriptor in CareKitStore use their OCKCD... counterparts instead of the value type counterparts.

Note

When I ran the test suite, none of the current tests for the framework hit the lines I changed which is probably why this wasn't caught 🙃. This is because the tests are conducted at a lower level than the value type representations, but solidifies that we should use the CoreData Entity representations for querying keyPaths: https://github.com/carekit-apple/CareKit/blob/7416cce107875e58d0dbfd54afb16e007ef79033/CareKitStore/CareKitStoreTests/Streaming/TestCoreDataQueryMonitor.swift#L65-L70

https://github.com/carekit-apple/CareKit/blob/7416cce107875e58d0dbfd54afb16e007ef79033/CareKitStore/CareKitStore/Streaming/CoreDataQueryMonitor.swift#L47-L63

cbaker6 avatar Jul 07 '24 02:07 cbaker6