Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FHIRStore Crash When Using CloudKit/CoreData #16

Closed
1 task done
advatar opened this issue Jan 16, 2024 · 9 comments · Fixed by #21
Closed
1 task done

FHIRStore Crash When Using CloudKit/CoreData #16

advatar opened this issue Jan 16, 2024 · 9 comments · Fixed by #21
Labels
bug Something isn't working

Comments

@advatar
Copy link

advatar commented Jan 16, 2024

Description

When collecting samples like this:

private var healthKit: HealthKit {
        HealthKit {
            CollectSamples(
                [
                    HKClinicalType(.allergyRecord),
                    HKClinicalType(.clinicalNoteRecord),
                    HKClinicalType(.conditionRecord),
                    HKClinicalType(.coverageRecord),
                    HKClinicalType(.immunizationRecord),
                    HKClinicalType(.labResultRecord),
                    HKClinicalType(.medicationRecord),
                    HKClinicalType(.procedureRecord),
                    HKClinicalType(.vitalSignRecord)
                ],
                predicate: HKQuery.predicateForSamples(
                    withStart: Date.distantPast,
                    end: nil,
                    options: .strictEndDate
                ),
                deliverySetting: .anchorQuery(saveAnchor: false)
                //deliverySetting: .manual(safeAnchor: false)
            )
        }
    }

I often get crashes ending with:

SwiftUI/FetchCommon.swift:51: Fatal error: Can only invoke this method on the main queue

The traceback is like:

image

Reproduction

See above

Expected behavior

Samples should be collected and added to the FHIRStore without crashing

Additional context

I am doing something similar to t:

Code of Conduct

  • I agree to follow this project's Code of Conduct and Contributing Guidelines
@advatar advatar added the bug Something isn't working label Jan 16, 2024
@PSchmiedmayer
Copy link
Member

@advatar Thank you for reporting the issue!

Can you share the project that you are working on so we can try to reproduce the issue; is it open-source? From the stacktrace, it looks like you are using or trying to modify LLMonFHIR?

@advatar
Copy link
Author

advatar commented Jan 16, 2024 via email

@advatar
Copy link
Author

advatar commented Jan 16, 2024

I modified LLMonFHIR like this:

.background(.afterAuthorizationAndApplicationWillLaunch, saveAnchor: true)

I did not get a crash and it works for one launch but next time I get "No health records are available"

btw, same thing with:

.anchorQuery(saveAnchor: true)

@advatar
Copy link
Author

advatar commented Jan 16, 2024

Hmm, I am using CoreData/CloudKit and noticed that the crash only happens if I do:

.environment(\.managedObjectContext, PersistenceController.shared.persistentContainer.viewContext)

on the HomeView

@PSchmiedmayer PSchmiedmayer changed the title Bug report: Remove this title with a descriptive title. FHIRStore Crash When Using CloudKit/CoreData Jan 16, 2024
@PSchmiedmayer
Copy link
Member

@advatar Can you share a minimally reproducible example that we can use to work on fixing the bug together? A very simple iOS app that just contains the necessary code to reproduce the results and only the necessary Stanford dependencies would be great. Ideally you can share it as an GitHub project; that would be perfect!

@advatar
Copy link
Author

advatar commented Jan 17, 2024

I will try. I live in Sweden and Clinical Records are not available in HealthKit. I am getting data from a national service, 23andme, microbiome and blood testing sites. Non of this is FHIR.

A couple of features that would be nice

  • generation of Clinical Records in FHIR from minimal data; date, quantity, value f.i (the rest of the decoration can be via the JSON)
  • differentiate between different types of Observations like Vital Signs vs Laboratory and Survey
  • Possibility to query for repeated laboratory test to get a series in chronological order

Anyways, this project is a few years old and used CoreData etc. Cleaning up all of that. At the moment it does not crash as often.

@advatar
Copy link
Author

advatar commented Jan 20, 2024

Just a quick question. I have now implemented generation of FHIRResources and can filter on vital-signs and laboratory. I works and I can view lab results. However, there is no persistence, I have to create the resources every time.

I am doing something like this:

let resourceProxies =  DemoManager.shared.loadLabResultsDemo()
                        for resourceProxy in resourceProxies {
                            if let observation = resourceProxy.get(if: ModelsR4.Observation.self) {
                                let fhirResource = FHIRResource(resource: observation, displayName: resourceProxy.displayName)
                                fhirStore.insert(resource: fhirResource)
                            }
                        }

Do I need something else than insert?

@advatar
Copy link
Author

advatar commented Jan 20, 2024

  • differentiate between different types of Observations like Vital Signs vs Laboratory and Survey

I did this for now:

extension Array where Element == FHIRResource {
    /// Filters the FHIR resources using the provided search text.
    /// - Parameter category: Filters the FHIR resources using the provided category string.
    /// - Returns: The filtered FHIR resources.
    public func filterByObservationCategory(with category: String) -> [FHIRResource] {
        if category.isEmpty {
            return self
        }
        
        return filter { resource in
            switch resource.versionedResource {
            case .r4(let r):
                if let o = r as? ModelsR4.Observation, let categoryString = o.category?.first?.coding?.first?.code?.value?.string {
                    return categoryString == category
                }
            case .dstu2(let r):
                if let o = r as? ModelsDSTU2.Observation, let categoryString = o.category?.coding?.first?.code?.value?.string {
                    return categoryString == category
                }
            }
            return false
        }
    }
}

@advatar
Copy link
Author

advatar commented Mar 16, 2024

This example is easily reproducible StanfordBDHG/LLMonFHIR#45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants