carekit-apple / CareKit

CareKit is an open source software framework for creating apps that help people better understand and manage their health.
https://www.researchandcare.org
Other
2.4k stars 443 forks source link

Issue with OCKSynchronizedStoreManager #714

Closed Bourne-alt closed 2 months ago

Bourne-alt commented 2 months ago

this is my code " lazy private(set) var synchronizedStoreManager: OCKSynchronizedStoreManager = { let coordinator = OCKStoreCoordinator() coordinator.attach(eventStore: healthKitStore) coordinator.attach(store: coreDataStore) return OCKSynchronizedStoreManager(wrapping: coordinator) }()" it throws a error "OCKSynchronizedStoreManager' is unavailable: OCKSynchronizedStoreManager and its related types are no longer available as a mechanism to synchronize with the CareKit store. As a replacement, see the asynchronous streams available directly on a CareKit store. For example, to monitor changes to tasks, see OCKStore.tasks(query:). " Any help would be greatly appreciated!

gavirawson-apple commented 2 months ago

Hi @Bourne-alt, the synchronized store manager has been recently deprecated in favor of a new API that uses async streams. The streams are available directly on the OCKStore and provide a nice way to track changes. What are you trying to accomplish? I can help provide some code snippets to guide you along.

Bourne-alt commented 2 months ago

Thanks, @gavirawson-apple, for your reply. I want to implement the functionality to sync user running data from HealthKit and display it using charts in my app.

gavirawson-apple commented 2 months ago

I see, so a few steps to get that set up.

  1. Create a HealthKit passthrough store. That will pull data directly from HealthKit and mark tasks as completed accordingly.
  2. Seed the store with some HealthKit linked tasks, in this case a task linked to steps data.
  3. Run a query in your view and map care events from the store to a SwiftUI chart. The events will get updated with HealthKit data when it's available. But note, if you're doing heavy computation work with the results consider moving to a view model (off the main thread) and use the async streams available on a care store to fetch and operate on care events.

Here's some code to get you started. I haven't filled out all of these steps, but it's a good starting point:

import CareKit
import CareKitStore
import CareKitUI
import Charts
import SwiftUI

struct MyApp: App {

    private let storeCoordinator: OCKStoreCoordinator = {

        let careStore = OCKStore(name: "Care Store")
        let healthKitPassthroughStore = OCKHealthKitPassthroughStore(store: careStore)

        let storeCoordinator = OCKStoreCoordinator()
        storeCoordinator.attach(eventStore: healthKitPassthroughStore)

        // TODO: Seed store with tasks

        return storeCoordinator
    }()

    var body: some Scene {
        WindowGroup {
            HomeScreen()
                .environment(\.careStore, storeCoordinator)
        }
    }
}

struct HomeScreen: View {

    // Fetch all of the events occurring today
    @CareStoreFetchRequest(query: OCKEventQuery(for: Date()))
    private var events

    var body: some View {

        Chart {

            // An example of computing progress that can be showed in a chart
            let overallProgress = events
                .map { $0.result.computeProgress(by: .checkingOutcomeExists).fractionCompleted }
                .reduce(0, +)

            let fractionCompleted = overallProgress / Double(events.count)

            // Generate chart using `events`
            // ...
        }
    }
}
marco-theraforge commented 2 months ago

Hi @Bourne-alt,

you may want to look at how we have done it with CK 2.0.2 in our MagicBox template app and our offline-first local and cloud storage:

https://github.com/TheraForge/Getting-Started https://github.com/TheraForge/Getting-Started https://github.com/TheraForge/OTFMagicBox https://github.com/TheraForge/OTFMagicBox

We target iOS 14+. For that you can use:

https://github.com/ChartsOrg/Charts https://github.com/ChartsOrg/Charts

We also have an extensible Design System to add more UI elements to the toolkit.

Hope this helps,

Marco

On 22 Jun 2024, at 05:42, Bourne-alt @.***> wrote:

Hi, thank you for your response. Running the code you provided will throw an error “Cannot find type ‘OCKStoreCoordinator’ in scope”? The CareKit version is 2.0.1.

import CoreData import SwiftUI import CareKit import CareKitStore import CareKitUI import Charts

struct ContentView: View { @State https://github.com/State private var selectedTab = 0 @State https://github.com/State private var chatMessage = "" @UIApplicationDelegateAdaptor(HealthGPTAppDelegate.self) var appDelegate @AppStorage https://github.com/AppStorage(StorageKeys.onboardingFlowComplete) var completedOnboardingFlow = false

private let storeCoordinator: OCKStoreCoordinator = {

let careStore = OCKStore(name: "Care Store")
let healthKitPassthroughStore = OCKHealthKitPassthroughStore(store: careStore)

let storeCoordinator = OCKStoreCoordinator()
storeCoordinator.attach(eventStore: healthKitPassthroughStore)

// TODO: Seed store with tasks

return storeCoordinator

}()

var body: some View { VStack(spacing: 0) { // Top Bar with Segmented Picker for Data and CHAT VStack { Picker(selection: $selectedTab, label: Text("")) { Text("数据").tag(0) Text("聊聊").tag(1) } .pickerStyle(SegmentedPickerStyle()) .padding(.horizontal) .padding(.top, 50) .background(Color(UIColor.systemGroupedBackground)) }

    // Search Bar
    if selectedTab == 0 {

        RunningDataView()
               .environment(\.careStore, storeCoordinator)
    }

    // Gemini Content
    if selectedTab == 1 {
        if completedOnboardingFlow {
            HealthGPTView()
        } else {
            EmptyView()
        }
    }

    Spacer()
}
.background(Color(UIColor.systemGroupedBackground))
.edgesIgnoringSafeArea(.top)

} }

Preview {

ContentView() }

— Reply to this email directly, view it on GitHub https://github.com/carekit-apple/CareKit/issues/714#issuecomment-2183760197, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNZRPH5BUK62GT2I7SAWNLZITXAXAVCNFSM6AAAAABJPXRQG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTG43DAMJZG4. You are receiving this because you are subscribed to this thread.

Bourne-alt commented 2 months ago

Hi @marco-theraforge This is what I wanted, it will be very helpful, thank you.

gavirawson-apple commented 2 months ago

Good to close this out @Bourne-alt or do you still need some guidance here?

Bourne-alt commented 2 months ago

Yes, we can close this out. Thank you for your help!