diff --git a/Examples/RemindersTests/SearchRemindersTests.swift b/Examples/RemindersTests/SearchRemindersTests.swift index 8faeceb1..aab566ef 100644 --- a/Examples/RemindersTests/SearchRemindersTests.swift +++ b/Examples/RemindersTests/SearchRemindersTests.swift @@ -28,38 +28,34 @@ extension BaseTestSuite { model.searchText = "Take" try await model.searchTask?.value #expect(model.searchResults.completedCount == 1) - withKnownIssue( - "'@Fetch' introduces an escaping closure and loses the task-local dependency" - ) { - assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) { - """ - [ - [0]: SearchRemindersModel.Row( - isPastDue: false, + assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) { + """ + [ + [0]: SearchRemindersModel.Row( + isPastDue: false, + notes: "", + reminder: Reminder( + id: UUID(00000000-0000-0000-0000-00000000000A), + dueDate: Date(2009-02-17T23:31:30.000Z), + isFlagged: false, notes: "", - reminder: Reminder( - id: UUID(00000000-0000-0000-0000-00000000000A), - dueDate: Date(2009-02-17T23:31:30.000Z), - isCompleted: false, - isFlagged: false, - notes: "", - position: 8, - priority: .high, - remindersListID: UUID(00000000-0000-0000-0000-000000000001), - title: "Take out trash" - ), - remindersList: RemindersList( - id: UUID(00000000-0000-0000-0000-000000000001), - color: 3985191935, - position: 2, - title: "Family" - ), - tags: "", - title: "**Take** out trash" - ) - ] - """ - } + position: 8, + priority: .high, + remindersListID: UUID(00000000-0000-0000-0000-000000000001), + status: .incomplete, + title: "Take out trash" + ), + remindersList: RemindersList( + id: UUID(00000000-0000-0000-0000-000000000001), + color: 3985191935, + position: 2, + title: "Family" + ), + tags: "", + title: "**Take** out trash" + ) + ] + """ } } @@ -67,62 +63,60 @@ extension BaseTestSuite { let model = SearchRemindersModel() model.searchText = "Take" try await model.showCompletedButtonTapped() + try await model.searchTask?.value + try await model.$searchResults.load() - withKnownIssue( - "'@Fetch' introduces an escaping closure and loses the task-local dependency" - ) { - assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) { - """ - [ - [0]: SearchRemindersModel.Row( - isPastDue: false, + assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) { + """ + [ + [0]: SearchRemindersModel.Row( + isPastDue: false, + notes: "", + reminder: Reminder( + id: UUID(00000000-0000-0000-0000-00000000000A), + dueDate: Date(2009-02-17T23:31:30.000Z), + isFlagged: false, notes: "", - reminder: Reminder( - id: UUID(00000000-0000-0000-0000-00000000000A), - dueDate: Date(2009-02-17T23:31:30.000Z), - isCompleted: false, - isFlagged: false, - notes: "", - position: 8, - priority: .high, - remindersListID: UUID(00000000-0000-0000-0000-000000000001), - title: "Take out trash" - ), - remindersList: RemindersList( - id: UUID(00000000-0000-0000-0000-000000000001), - color: 3985191935, - position: 2, - title: "Family" - ), - tags: "", - title: "**Take** out trash" + position: 8, + priority: .high, + remindersListID: UUID(00000000-0000-0000-0000-000000000001), + status: .incomplete, + title: "Take out trash" + ), + remindersList: RemindersList( + id: UUID(00000000-0000-0000-0000-000000000001), + color: 3985191935, + position: 2, + title: "Family" ), - [1]: SearchRemindersModel.Row( - isPastDue: false, + tags: "", + title: "**Take** out trash" + ), + [1]: SearchRemindersModel.Row( + isPastDue: false, + notes: "", + reminder: Reminder( + id: UUID(00000000-0000-0000-0000-000000000006), + dueDate: Date(2008-08-07T23:31:30.000Z), + isFlagged: false, notes: "", - reminder: Reminder( - id: UUID(00000000-0000-0000-0000-000000000006), - dueDate: Date(2008-08-07T23:31:30.000Z), - isCompleted: true, - isFlagged: false, - notes: "", - position: 4, - priority: nil, - remindersListID: UUID(00000000-0000-0000-0000-000000000000), - title: "Take a walk" - ), - remindersList: RemindersList( - id: UUID(00000000-0000-0000-0000-000000000000), - color: 1218047999, - position: 1, - title: "Personal" - ), - tags: "#car #kids #social", - title: "**Take** a walk" - ) - ] - """ - } + position: 4, + priority: nil, + remindersListID: UUID(00000000-0000-0000-0000-000000000000), + status: .completed, + title: "Take a walk" + ), + remindersList: RemindersList( + id: UUID(00000000-0000-0000-0000-000000000000), + color: 1218047999, + position: 1, + title: "Personal" + ), + tags: "#car #kids #social", + title: "**Take** a walk" + ) + ] + """ } } @@ -131,40 +125,37 @@ extension BaseTestSuite { model.searchText = "Take" try await model.showCompletedButtonTapped() model.deleteCompletedReminders() + try await model.searchTask?.value try await model.$searchResults.load() #expect(model.searchResults.completedCount == 0) - withKnownIssue( - "'@Fetch' introduces an escaping closure and loses the task-local dependency" - ) { - assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) { - """ - [ - [0]: SearchRemindersModel.Row( - isPastDue: false, + assertInlineSnapshot(of: model.searchResults.rows, as: .customDump) { + """ + [ + [0]: SearchRemindersModel.Row( + isPastDue: false, + notes: "", + reminder: Reminder( + id: UUID(00000000-0000-0000-0000-00000000000A), + dueDate: Date(2009-02-17T23:31:30.000Z), + isFlagged: false, notes: "", - reminder: Reminder( - id: UUID(00000000-0000-0000-0000-00000000000A), - dueDate: Date(2009-02-17T23:31:30.000Z), - isCompleted: false, - isFlagged: false, - notes: "", - position: 8, - priority: .high, - remindersListID: UUID(00000000-0000-0000-0000-000000000001), - title: "Take out trash" - ), - remindersList: RemindersList( - id: UUID(00000000-0000-0000-0000-000000000001), - color: 3985191935, - position: 2, - title: "Family" - ), - tags: "", - title: "**Take** out trash" - ) - ] - """ - } + position: 8, + priority: .high, + remindersListID: UUID(00000000-0000-0000-0000-000000000001), + status: .incomplete, + title: "Take out trash" + ), + remindersList: RemindersList( + id: UUID(00000000-0000-0000-0000-000000000001), + color: 3985191935, + position: 2, + title: "Family" + ), + tags: "", + title: "**Take** out trash" + ) + ] + """ } } } diff --git a/Sources/SQLiteData/Internal/FetchKey.swift b/Sources/SQLiteData/Internal/FetchKey.swift index 1b45d53b..1ed414cd 100644 --- a/Sources/SQLiteData/Internal/FetchKey.swift +++ b/Sources/SQLiteData/Internal/FetchKey.swift @@ -53,6 +53,7 @@ struct FetchKey: SharedReaderKey { #if DEBUG let isDefaultDatabase: Bool #endif + @Dependency(\.self) var dependencies public typealias ID = FetchKeyID @@ -86,18 +87,22 @@ struct FetchKey: SharedReaderKey { return } let scheduler: any ValueObservationScheduler = scheduler ?? ImmediateScheduler() - database.asyncRead { dbResult in - let result = dbResult.flatMap { db in - Result { - try request.fetch(db) + withEscapedDependencies { dependencies in + database.asyncRead { dbResult in + let result = dbResult.flatMap { db in + Result { + try dependencies.yield { + try request.fetch(db) + } + } } - } - scheduler.schedule { - switch result { - case .success(let value): - continuation.resume(returning: value) - case .failure(let error): - continuation.resume(throwing: error) + scheduler.schedule { + switch result { + case .success(let value): + continuation.resume(returning: value) + case .failure(let error): + continuation.resume(throwing: error) + } } } } @@ -111,8 +116,12 @@ struct FetchKey: SharedReaderKey { return SharedSubscription {} } #endif - let observation = ValueObservation.tracking { db in - Result { try request.fetch(db) } + let observation = withEscapedDependencies { dependencies in + ValueObservation.tracking { db in + dependencies.yield { + Result { try request.fetch(db) } + } + } } let scheduler: any ValueObservationScheduler = scheduler ?? ImmediateScheduler()