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

Use CareKit 3.0.0 #116

Merged
merged 54 commits into from
May 2, 2023
Merged

Use CareKit 3.0.0 #116

merged 54 commits into from
May 2, 2023

Conversation

cbaker6
Copy link
Member

@cbaker6 cbaker6 commented Apr 9, 2023

A nice code snippet that can be used in the future for streaming CareKitStore data in a View Model to a View is below:

private func fetchTasks(on date: Date) async -> ([OCKAnyTask], OCKTaskQuery) {
        var query = OCKTaskQuery(for: date)
        query.excludesTasksWithNoEvents = true
        do {
            let tasks = try await store.fetchAnyTasks(query: query)
            let orderedTasks = TaskID.ordered.compactMap { orderedTaskID in
                tasks.first(where: { $0.id == orderedTaskID }) }
            return (orderedTasks, query)
        } catch {
            Logger.feed.error("\(error, privacy: .public)")
            return ([], query)
        }
    }

    private func streamEvents(on date: Date) async throws -> CareStoreQueryResults<OCKAnyEvent> {
        let (tasks, taskQuery) = await self.fetchTasks(on: date)
        return try streamCoordinatorEvents(for: tasks, from: taskQuery)
    }

    private func streamCoordinatorEvents(for tasks: [OCKAnyTask],
                                         from query: OCKTaskQuery) throws -> CareStoreQueryResults<OCKAnyEvent> {
        guard tasks.count > 0 else {
            throw AppError.errorString("No current tasks")
        }
        guard let dateInterval = query.dateInterval else {
            throw AppError.errorString("Task query should have a set date")
        }
        var eventQuery = OCKEventQuery(dateInterval: dateInterval)
        eventQuery.taskIDs = tasks.map { $0.id }
        guard let store = store as? OCKStoreCoordinator else {
            throw AppError.couldntBeUnwrapped
        }
        return store.anyEvents(matching: eventQuery)
    }

}
guard careViewController.store !== appDelegate.storeCoordinator else {
// No need to replace view
// careViewController.events = events
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out this line to prevent app crashing in Xcode 14.3 due to SwiftUI card and UIKit cards streaming in the same view but called on different queues, resulting in: error: NULL _cd_rawData but the object is not being turned into a fault CoreData: error: NULL _cd_rawData but the object is not being turned into a fault

event.computeProgress(by: .checkingOutcomeExists)
}) {
super.init(store: store, computeProgress: computeProgress)
// self.events = events
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the other line commented out due to app crash, uncomment both lines to re-enable Steps card

@cbaker6 cbaker6 merged commit 7d897fd into main May 2, 2023
@cbaker6 cbaker6 deleted the newCareKit branch May 2, 2023 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant